Is there a way to get the state of a random number generator?

Say I'm seed 123 with srand(123) , and run rand() X times. Later, I want to be able to restart Julia and sow a number (or state), so when I run rand() again, I get a number that would be generated if I had 123 seed and ran rand() X + 1 times Is there a way I can do this, or do I really need to run rand() X times to get the state I want?

+5
source share
1 answer

If the solution with a custom random number generator presented in Extract RNG seed in julia is not practical for you, so I could come up with to copy the entire structure of the global random number generator:

 function reset_global_rng(rng_state) Base.Random.GLOBAL_RNG.seed = rng_state.seed Base.Random.GLOBAL_RNG.state = rng_state.state Base.Random.GLOBAL_RNG.vals = rng_state.vals Base.Random.GLOBAL_RNG.idx = rng_state.idx end rs = deepcopy(Base.Random.GLOBAL_RNG) println(rand(5)) # [0.301558,0.602108,0.220952,0.0338732,0.553414] reset_global_rng(rs) println(rand(5)) # [0.301558,0.602108,0.220952,0.0338732,0.553414] 

although I'm not 100% sure how it does not interact with dsfmt_gv_srand() in random.jl.

+5
source

Source: https://habr.com/ru/post/1266284/


All Articles