When you execute a program that uses math.random
, without setting math.randomseed
, it will usually yield the same set of random numbers. This is due to the fact that math.randomseed
is responsible for setting the default seed
(or algorithm-generator) for the random numbers brought out by math.random
.
This consistency obviously is not random. Allow me to give an example - First go to the Lua Demo Website and then insert this piece of code:
for i = 1,10 do
print(math.random())
end
Keep on consistently hitting the run button and see how the interpreter will yield the same numbers each time. However, to change the 'seed' by which the random numbers are being generated, we can just set the 'seed' to whatever the current time is (since current time never repeats)
This time go on the website and execute this code multiple times:
math.randomseed(os.time())
for i = 1,10 do
print(math.random())
end
You shall now note how you will get different numbers each time.