Random numbers: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Blobyblobyblob
No edit summary
m Fix code formatting
 
(52 intermediate revisions by 14 users not shown)
Line 1: Line 1:
'''This article may have a few very confusing parts... Try to ignore the stuff you don't understand, and rather just try the step-by-step instructions further down'''
{{Map|Tutorials}}
==Introduction==
There is a random number generator in Roblox that seems to generate random numbers.  
But do a test, and insert this script into your place:
{{code|=
for _ = 1, 10 do
print(math.random(100))
wait(1)
end
}}
Test the place, and look at the [[Scripting|output window]]. Write down the numbers you get, and exit the test. Then do the test again, and again write down the numbers you get. Notice something weird? Those 10 very random numbers are similar everytime you run a test (or play online for that matter).


== Not really random ==
This article explains how to achieve truly random numbers that are not the same every time a place is played.
The random number generator in roblox does seem to generate random numbers.  
==The General Idea==
But do a test, and insert this script into your place:<br/>
Every time a script in a place uses the math.random function, the random generator generates a random number. However, the random number generator is deterministic. It uses the last random number to generate the next random number. When the scripts starts, the "first" random number is always the same. So every time the script is ran, the same random number sequence will be generated, as in the test at the top of this page.
''for i = 1, 10 do''<br/>
==Generating Different Random Numbers Every Time the Game Runs==
''print(math.random(1,100))''<br/>
To receive a different value every time the function is called, you need to set the seed to a different value when the game runs or to call the math.random function a random number of times, if you want to get different numbers every time. Some truly random events can be things like:
''wait(1)''<br/>
''end''<br/>


Test the place, and look at the [[Scripting|output window]]. Write down the numbers you get, and exit the test. Then do the test again, and again write down the numbers you get. Notice something weird? Those 10 very random numbers are exactly the same everytime you run a test (or play online for that matter).
* Players' movement speed
 
'''This article explains how to achieve truly random numbers that are NEVER the same for every time a place is played'''
 
== The General idea ==
 
Everytime a script in a place does math.random(), the random generator generates a random number. If this is done at exactly the same time, with exactly the same random numbers everytime, then the random number sequence will be the same every time (like the test at the top of this page).<br/>
However, if you were able to make the place generate random numbers at random times (in addition to your normal random generations), then the sequence of random numbers would change, right? Yes! <br/>
But the problem is (difficult sentence warning), if the randomly generated randomizing of the random number generator is the same everytime it randomizes something, then the sequence will still be the same every time!
Ok, that was a difficult sentence to swallow.....
 
'''If you are confused by now, try to ignore the parts you don't understand, and just follow the techniques below!'''
 
== How to generate TRULY random numbers ==
 
You need to make a script do math.random(x,y) at atleast one TRULY random time, and then the rest of the randomized numbers will be truly random. Scripting is, of course, needed to make this happen. Some truly random events can be things like:
* Number of players in your place
* Players movement
* Other real-life influences on the game
* Other real-life influences on the game
* The chances of getting an item or map with the usage of a table


There are 3 things this can influence that in general will make stuff very random:
There are 2 ways you can make the random number generator generate different numbers every time the place runs:
'''math.random(x,y)'''
* call math.random a random (from real-life influences) number of times
* If you change either x or y (random numbers min and max)
* Seed the random generator with a different number whenever the game runs
* If you change when the math.random happens
* If you change how often the math.random happens
 
If you can find a way to make something truly random influence any of the three listed items, then you will be able to generate truly random numbers!
 
=== Example 1 ===
 
Have a script in workspace that checks for number of players in the game every 10 seconds or so, then does a math.random() code based on the number of players.<br/><br/>
''while true do''<br/>
''players = game.Players:getChildren()''<br/>
''wait(10)''<br/>
''if #players > 0 then''<br/>
''math.random(1, #players)''<br/>
''end''<br/><br/>
"#players" means "number of players". We don't need to remember the random number we generate. It is enough to just generate a number to make the random number generator randomized. Also note, that if the number of players in a place is the same at all the same times every time, then the random numbers will be the same every time too.
 
=== Example 2 ===


''You don't really need to do anything more than in Example 1, but these extra examples are included for the more interested people.''<br/>
If you can find a way to make something truly random influence any of the two listed items, then you will be able to generate different numbers every time the game runs.
This script is even more randomized, and even randomizes the time between each randomization:<br/><br/>
===math.randomseed===
''while true do''<br/>
The best way to fix this problem is setting the seed of math.random() with the function called math.randomseed().
''players = game.Players:getChildren()''<br/>
''wait(math.random(1,#players))''<br/>
''if #players > 0 then''<br/>
''math.random(1, #players)''<br/>
''end''<br/><br/>


Also here, note, that if the number of players in a place is the same at all the same times every time, then the random numbers will be the same every time too.
The problem is finding a good seed. One option is to use [[Function_Dump/Roblox_Specific_Functions#tick.28.29|tick()]]. This returns the number of seconds elapsed since UNIX time (1/1/1970 0:00:00).


=== Example 3 ===
To use it, simply pass the result of tick() to math.randomseed.
''You don't really need to do anything more than in Example 1, but these extra examples are included for the more interested people.''<br/>
<syntaxhighlight lang="lua">math.randomseed(tick())</syntaxhighlight>
Alternatively, you could randomize numbers based on players movement by putting this touch-script into a brick that players walk across now and then (maybe a baseplate?)<br/><br/>
===Using the Seed===
''local debounce = 0''<br/>
You could use math.randomseed anywhere. It is recommended to put it at the beginning of the script (it doesn't really matter, as long as you put it before you use math.random). Now, let's use the experiment script that we used before and use math.randomseed with it!
''function onTouch(hit)''<br/>
''if debounce == 0 then''<br/>
''debounce = 1''<br/>
''if hit.Parent:findFirstChild("Humanoid") ~= nil then''<br/>
''math.random(1,100)''<br/>
''end''<br/>
''debounce = 0''<br/>
''end''<br/>
''end''<br/><br/>
''script.Parent.Touched:connect(onTouch)''<br/><br/>


Everytime someone steps on this brick, the random-number generator will be randomized.
{{code|=
math.randomseed(tick())
for _ = 1, 10 do
print(math.random(100))
wait(1)
end
}}


==Easiest Way!!!==
Congratulations! Your random numbers will now be different every time the place runs!
Put something like this in a script
==Even more random==
When doing something like math.random(1, 100), all the numbers will turn out as whole numbers, which in some cases, just isn't enough. However, if you did this, then the numbers would be random non-integers between 1 and 100, including decimals:


{{code|=
math.randomseed(tick())


while true do<br>
for _ = 1, 10 do
wait(3)
print(math.random()*100)
math.random(0,1)<br>
wait(1)
end<br>
end
}}


This proves that when calling the math.random() function with no arguments provided will give you a number between 0 and 1.


Now was that so hard???
[[Category:Scripting Tutorials]]

Latest revision as of 21:51, 30 July 2024

Introduction

There is a random number generator in Roblox that seems to generate random numbers. But do a test, and insert this script into your place:

for _ = 1, 10 do
	print(math.random(100))
	wait(1)
end

Test the place, and look at the output window. Write down the numbers you get, and exit the test. Then do the test again, and again write down the numbers you get. Notice something weird? Those 10 very random numbers are similar everytime you run a test (or play online for that matter).

This article explains how to achieve truly random numbers that are not the same every time a place is played.

The General Idea

Every time a script in a place uses the math.random function, the random generator generates a random number. However, the random number generator is deterministic. It uses the last random number to generate the next random number. When the scripts starts, the "first" random number is always the same. So every time the script is ran, the same random number sequence will be generated, as in the test at the top of this page.

Generating Different Random Numbers Every Time the Game Runs

To receive a different value every time the function is called, you need to set the seed to a different value when the game runs or to call the math.random function a random number of times, if you want to get different numbers every time. Some truly random events can be things like:

  • Players' movement speed
  • Other real-life influences on the game
  • The chances of getting an item or map with the usage of a table

There are 2 ways you can make the random number generator generate different numbers every time the place runs:

  • call math.random a random (from real-life influences) number of times
  • Seed the random generator with a different number whenever the game runs

If you can find a way to make something truly random influence any of the two listed items, then you will be able to generate different numbers every time the game runs.

math.randomseed

The best way to fix this problem is setting the seed of math.random() with the function called math.randomseed().

The problem is finding a good seed. One option is to use tick(). This returns the number of seconds elapsed since UNIX time (1/1/1970 0:00:00).

To use it, simply pass the result of tick() to math.randomseed.

math.randomseed(tick())

Using the Seed

You could use math.randomseed anywhere. It is recommended to put it at the beginning of the script (it doesn't really matter, as long as you put it before you use math.random). Now, let's use the experiment script that we used before and use math.randomseed with it!

math.randomseed(tick())
for _ = 1, 10 do
	print(math.random(100))
	wait(1)
end

Congratulations! Your random numbers will now be different every time the place runs!

Even more random

When doing something like math.random(1, 100), all the numbers will turn out as whole numbers, which in some cases, just isn't enough. However, if you did this, then the numbers would be random non-integers between 1 and 100, including decimals:

math.randomseed(tick())

for _ = 1, 10 do
	print(math.random()*100)
	wait(1)
end

This proves that when calling the math.random() function with no arguments provided will give you a number between 0 and 1.