According to the wiki, math.randomseed
sets the pseudo-random generator to determine the sequence of numbers math.random
will generate.
But...
It shows us how to set the seed, but is it possible to get the seed? What use does the math.randomseed
argument have (besides passing the timestamp tick
), if we can't get the seed of the current environment.
What if I said something like:
for i = 1,5 do print(math.random(10)) end
And gave an output of:
3
7
5
1
9
Is there a way I could get the random seed
from this environment, so I could then pass it as an argument
to math.randomseed
in another script, so it would generate the same number pattern in that situation?
math.randomseed(x) Sets x as the "seed" for the pseudo-random generator: equal seeds produce equal sequences of numbers.
Because you must explicitly set the seed, you could just record the value of x
as you set it.
function setRandomSeed(seed) local seed = seed or tick() math.randomseed(seed) return seed end
As for getting the seed from a list of numbers:
math.random
works by applying a series of mathematical operations given a value and a seed in order to generate a seemingly random sequence of numbers. However, in order to retrieve the seed, you would need to know what these operations are in order to complete the inverse operations and retrieve the seed.
I'm sure there is some source code out there somewhere were you could derive a formula to find the seed, but it is much more convenient and efficient to simply record which seed is being used.