Random numbers: Difference between revisions
>Sduke524 No edit summary |
>NXTBoy Lots of misconceptions here. Timing and arguments to math.random are irrelevant |
||
Line 3: | Line 3: | ||
There is a random number generator in Roblox that seems to generate random numbers. | 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:<br/> | But do a test, and insert this script into your place:<br/> | ||
for i = 1, 10 do | for i = 1, 10 do | ||
print(math.random( | print(math.random(100)) | ||
wait(1) | wait(1) | ||
end | 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). | 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). | ||
Line 16: | Line 15: | ||
== The General idea == | == The General idea == | ||
Everytime a script in a place does math.random(), the random generator generates a random number. | Everytime a script in a place does math.random(), 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 run, the same random number sequence will be generated, as in the test at the top of this page. | ||
== How to generate TRULY random numbers == | == How to generate TRULY random numbers == | ||
Line 27: | Line 24: | ||
* Other real-life influences on the game | * Other real-life influences on the game | ||
There are | There are 2 ways you can increase the randomness of the random number generator: | ||
* call math.random() a random (from real-life influences) number of times | |||
* Seed the random generator with a truly random number | |||
* | |||
If you can find a way to make something truly random influence any of the | 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 truly random numbers! | ||
===math.randomseed()=== | ===math.randomseed()=== | ||
The best way to fix this problem is a function called math.randomseed(). What the function does is set a seed for the random number generator. | The best way to fix this problem is a function called math.randomseed(). What the function does is set a seed for the random number generator. | ||
The problem is finding a good seed. | The problem is finding a good seed. One option is to s=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). | ||
To use it, simply pass the result of tick() to math.randomseed() parenthesis. | |||
math.randomseed(tick()) | |||
===Using the Seed=== | ===Using the Seed=== | ||
You could put math.randomseed() anywhere. It is recommended to put it at the beginning of the script (it doesn't really matter, as long as if you put it before you use math.randomseed()). Now, let's use the experiment script that we used before and use math.randomseed with it! | You could put math.randomseed() anywhere. It is recommended to put it at the beginning of the script (it doesn't really matter, as long as if you put it before you use math.randomseed()). Now, let's use the experiment script that we used before and use math.randomseed with it! | ||
math.randomseed(tick()) | math.randomseed(tick()) | ||
for i = 1, 10 do | for i = 1, 10 do | ||
print(math.random(1,100)) | print(math.random(1,100)) | ||
wait(1) | wait(1) | ||
end | end | ||
Congratulations! Your random numbers will now be much more random than before! | Congratulations! Your random numbers will now be much more random than before! | ||
== Even more random == | == Even more random == | ||
When doing something like math.random(1,100) all the numbers will turn out as whole numbers | 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 were to do | ||
for i = 1, 10 do | math.randomseed(tick()) | ||
print(math.random()*100 | |||
wait(1) | for i = 1, 10 do | ||
end | print(math.random()*100) | ||
wait(1) | |||
end | |||
The numbers will be | The numbers will be random non-integers between 1 and 100. | ||
[[Category:Scripting Tutorials]] | [[Category:Scripting Tutorials]] |
Revision as of 08:39, 17 July 2011
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 i = 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 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. 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 run, the same random number sequence will be generated, as in the test at the top of this page.
How to generate TRULY random numbers
You need to make a script do math.random(x,y) at at least 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
There are 2 ways you can increase the randomness of the random number generator:
- call math.random() a random (from real-life influences) number of times
- Seed the random generator with a truly random number
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 truly random numbers!
math.randomseed()
The best way to fix this problem is a function called math.randomseed(). What the function does is set a seed for the random number generator.
The problem is finding a good seed. One option is to s=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() parenthesis.
math.randomseed(tick())
Using the Seed
You could put math.randomseed() anywhere. It is recommended to put it at the beginning of the script (it doesn't really matter, as long as if you put it before you use math.randomseed()). Now, let's use the experiment script that we used before and use math.randomseed with it!
math.randomseed(tick())
for i = 1, 10 do print(math.random(1,100)) wait(1) end
Congratulations! Your random numbers will now be much more random than before!
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 were to do
math.randomseed(tick()) for i = 1, 10 do print(math.random()*100) wait(1) end
The numbers will be random non-integers between 1 and 100.