Lua random number? [duplicate]
Solution 1:
You'll want to seed your random number generator by calling math.randomseed()
before calling math.random()
. It's pretty common to use os.time()
as the seed value (math.randomseed(os.time())
).
It's important to note that math.random()
is deterministic so the entropy has to come from the seed value. If you pass the same value to the seed you'll get the same value(s) math.random()
. Since os.time()
only has resolution down to seconds which means that if you invoke the command multiple times within a given second you'll get the same values back. You can try using more entropic sources for seeding (/dev/random
) if you like.
And just to clarify, you can't guarantee that the values will be different every time if it's truly random. All you can do is ensure that there's a sufficiently low probability that you'll get the same values..