Get the current random seed from lua
I know you can set the seed of lua random number generator with math.randomseed()
.
I want to generate two separate deterministic sequences of numbers somewhat in parallel. I reduced my problem to something like 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 went through the documentation and threw the table math
into a k,v in pairs
for loop and nothing came up. Does such a function exist in lua, or should I write my own generator for this purpose?
source to share
There are currently no functions in the Lua standard library that get the current seed. To do this, you need to either:
-
Write your own RNG function ( tricky )
-
Modify the function
math.randomseed
written in C ( risky ) -
Use RNG library which has this function ( simple )
-
Store the number of times
math.random
called in a variable ( simplest, but not ideal )
Custom RNG
Writing your own RNG will be time consuming, error prone, and not optimized. You can search the internet for algorithms on how to do this, but each one will have different quirks. Some trading characteristics are for randomness, while other random number patterns are potentially predictable.
Edit math.randomseed
in standard library
This could potentially compromise any third party modules or libraries that use the library math
. Not to mention, there can be a problem with Lua itself if you don't know what you are doing.
Download RNG
Several RNG modules are available in Lua. I highly recommend that you choose the one that suits your needs and make sure it is capable of receiving the current seed. rotLove has several that do this, but there is a caveat against using them. An RNG segment installed for one of the modules will not seed math.randomseed
.
A relevant story about this ...
This issue recently happened to someone who tried to use both the RNG from rotLove and the dice module I developed. Bones were used math.random
to identify rolls. The person set the seed with RNG.randomseed
and assumed it would affect all operations math.random
only to find that each run time of their application gave the same results in the bone. This was because they never installed math.randomseed
! Because of this, I added a dice module to rotLove so that it uses the RNGs provided to generate dice rolls instead of juggling with third-party RNGs and standard LUN RNGs.
Save variable
After you set the seed, each time it math.random
is called, increment the variable by +1. When you just need to get the seed,
math.randomseed(orginal_seed_provided)
seed_counter = 0 -- every time math.random is called add one
-- whenever you need the current seed, it 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 the seed that has been buried.
Once you get the seed after being math.random
invoked 100,000+ times , it will drain the processor with a noticeable effect, so BEWARE.
source to share