Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

math.random isn't random question?

Asked by 7 years ago

So I've noticed that if you do

while true do
wait(1)
print(math.random(1,20))
end

then look at the output and study those numbers for a few seconds. They will always be the same numbers every time you re-run the server... Is there a way to make math.random have TRULEY random numbers?

Thanks.

1 answer

Log in to vote
6
Answered by 7 years ago
Edited 7 years ago

Seed

Each random number returned from random is part of a sequence generated by a given seed. While the numbers may appear random, they're really just giving you whatever is calculated next in the sequence generated from it's seed. In other words: same seed, same number sequence.


Why is this relevant?

This is important to understand, because a default seed is given to the RNG (Random Number Generator) each time your program starts. This seed is immutable.

So how do I fix this?

The solution is a function called randomseed of Lua's math library. Despite it's name, it doesn't actually generate a random seed. It just implies it's going to set a new seed for the RNG. This means you must come up with the seed (which is just a number), then pass it to the randomseed function.

This function will give the RNG the seed you provided, and generate a new random number sequence based off of it. Here's an example:

math.randomseed(100) -- "100" being the new seed the RNG has to work with.
print(math.random(999)) -- This will always print "12" the first time it's ran.

However, note what I said before: "same seed, same number sequence". As demonstrated in the code above, this will not fix your problem because you'd be using the same seed each time the program ran, giving you the same number sequence. This means you'd need a different seed each time the program ran. What could we use that constantly changes...? Time!


The one thing that changes every instance in existence, is time. And how convenient, we can represent time with numbers! Functions like os.time or tick returns it's respective time stamp in seconds. This makes os.time or tick the perfect seed for math.randomseed, since it will be different each time your program runs! Here's an example:

math.randomseed(tick()) -- Who knows what tick() will be!
print(math.random(100)) -- Unpredictable random number each time the program runs!

So now you know how math.randomseed works, and a bit more about how RNG works. You should note however, math.randomseed shouldn't need to be used more than once in a program in your specific situation. The only time you should ever need to use math.randomseed more than once in your program, is if you're depending on the seed for math.random to give you predictable numbers.


Hope this helped, let me know if you have any questions!

3
But, in the end, it's never random. It's only an illusion to make it look random. EzraNehemiah_TF2 3552 — 7y
Ad

Answer this question