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?
It is possible to hack into the Random Number Generators used in casinos and other fields. But, it is a difficult venture that even the best hackers find challenging. With high-quality RNGs and security protocols, this possibility can be reduced to the minimum.
We do this by shuffling the numbers as we generate the random numbers, instead of doing it beforehand. This is done by keeping track of how many numbers we've generated so far, and for each new number, we pick a random one from the unused set, swap it into the used set and then return it.
random seed() example to generate the same random number every time. If you want to generate the same number every time, you need to pass the same seed value before calling any other random module function.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With