Reputation: 118
I know that you can set the seed of lua's random number generator with math.randomseed()
.
I want to generate two separate deterministic sequences of numbers somewhat in parallel. I have reduced my problem to something to the effect of this:
-- assume seed1 and seed2 exist and are positive integers.
local sequence1 = {};
local sequence2 = {};
math.randomseed(seed1);
for i = 1,50 do
sequence1[#sequence1 + 1] = math.random();
end
seed1 = math.getseed(); -- This is the function that I want.
-- stuff
math.randomseed(seed2);
for i = 1,75 do
sequence2[#sequence2 + 1] = math.random();
end
seed2 = math.getseed(); -- This is the function that I want.
-- stuff
math.randomseed(seed1);
for i = 1,50 do
sequence1[#sequence1 + 1] = math.random();
end
seed1 = math.getseed(); -- This is the function that I want.
I have looked through the documentation and thrown the math
table into a k,v in pairs
for loop and nothing has come up.
Does any such function exist in lua, or must I write my own generator for this purpose?
Upvotes: 4
Views: 2341
Reputation: 56
Currently there are no functions in the standard Lua library that get the current seed. To do this you would either need to:
Write your own RNG function (difficult)
Alter the math.randomseed
function written in C (risky)
Use a RNG lib that has this feature (simple)
Save the number of times math.random
is called to a variable (simplest but not ideal)
Writing your own RNG would be time consuming, prone to error, and not optimized. You can search for algorithms on how to do this online, but they will each have their different quirks. Some trade performance for randomness, while others random number patterns are potentially predictable.
math.randomseed
in the standard libraryDoing this could potentially jeopardize any 3rd party modules or libraries that use the math
library. Not to mention this could cause problems with Lua itself if you don't know what you are doing.
There are multiple RNG modules written in Lua available. I'd strongly advise you to select one that suits your needs and make sure it is capable of getting the current seed. rotLove has multiple ones that do this, but there is a caveat to using them. The RNG seed you set for one of the modules will not seed math.randomseed
.
This problem recently happened to someone who tried to use both a RNG from rotLove and a dice module I had developed. The dice used math.random
to determine rolls. The person had set the seed with RNG.randomseed
and assumed it would affect all math.random
operations only to find that every runtime of their application yielded the same dice results. This was because they never set math.randomseed
! Because of this I added my dice module to rotLove so that it would use the RNGs provided to generate dice rolls, instead of having to juggle a third party RNG and the default Lua RNG.
After you set the seed, every time math.random
is called increase the variable by +1. When you need to get the seed just,
math.randomseed(orginal_seed_provided)
seed_counter = 0 -- every time math.random is called add one
-- whenever you need the current seed, it's the seed_counter
-- run code
-- suddenly you need to reset RNG to a previous seed
math.randomseed(orginal_seed_provided)
for i=1, seed_counter do
math.random() -- discarding the results from prior math.random operations
end
-- next call to math.random() will be where seed left off
This option has dire consequences because the more you use math.random()
the more function calls you have to make to get to a seed that is buried.
Once you are getting a seed after math.random
has been called 100,000+ times it will be a CPU drain with noticeable effect so BEWARE.
Upvotes: 2