Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to obtain the state of the random number generator? [duplicate]

Tags:

random

julia

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?

like image 916
amrods Avatar asked Apr 04 '17 02:04

amrods


People also ask

Can you trick a random number generator?

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.

How will you generate a random number which does not get repeated?

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.

Can you generate the same random numbers everytime?

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.


1 Answers

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.

like image 65
Bogumił Kamiński Avatar answered Nov 03 '22 02:11

Bogumił Kamiński