Random numbers: Difference between revisions
>JulienDethurens No edit summary |
>Flurite No edit summary |
||
Line 4: | Line 4: | ||
But do a test, and insert this script into your place: | But do a test, and insert this script into your place: | ||
{{code|= | {{code|= | ||
for | for number = 1, 10 do | ||
print(math.random(100)) | print(math.random(100)) | ||
wait(1) | wait(1) | ||
Line 13: | Line 13: | ||
This article explains how to achieve truly random numbers that are not the same every time a place is played. | This article explains how to achieve truly random numbers that are not the same every time a place is played. | ||
==The General Idea== | ==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== | ==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 | * Players' movement speed | ||
* 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 2 ways you can make the random number generator generate different numbers every time the place runs: | There are 2 ways you can make the random number generator generate different numbers every time the place runs: | ||
Line 26: | Line 27: | ||
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. | 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=== | ===math.randomseed=== | ||
The best way to fix this problem is | 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 [[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). | 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). | ||
Line 45: | Line 46: | ||
Congratulations! Your random numbers will now be different every time the place runs! | Congratulations! Your random numbers will now be different every time the place runs! | ||
==Even more random== | ==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: | 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|lua= | {{code|lua= | ||
Line 55: | Line 56: | ||
end | end | ||
}} | }} | ||
This proves that when calling the math.random() function with no arguments provided will give you a number between 0 and 1. | |||
[[Category:Scripting Tutorials]] | [[Category:Scripting Tutorials]] |
Revision as of 23:22, 5 April 2012
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 number = 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 i = 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:
{{{}}}
This proves that when calling the math.random() function with no arguments provided will give you a number between 0 and 1.