This question already has an answer here:
-
Retrieve RNG seed in julia
5 answers
Say I seed 123 with srand(123)
, and run rand()
X times. Later, I want to be able to restart Julia and seed a number (or state) such that when I run rand()
again I get the number that would have been generated if I had seed 123 and run rand()
X + 1 times. Is there any way I can do that, or do I really have to run rand()
X times to obtain the state I want?
If the solution with custom random number generator presented in Retrieve RNG seed in julia is not feasible for you the best I can come up with is to copy the whole structure of 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 am not 100% sure how it does not come into interaction with dsfmt_gv_srand()
in random.jl.