On Stackoverflow there is a post showing how to save the current state of the base random number generator, so that one can go back and produce the same set of random numbers from a set point that is already so many draws in (ie it is not a simple set the seed response).

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

using Distributions

rs = deepcopy(Base.Random.GLOBAL_RNG)

println(rand(5))

reset_global_rng(rs)

println(rand(5))

This does not work for distributions such as Beta and Gamma which utilise the StatsFuns package random number generator instead of the Base one.

Iâ€™ve tried to write a function similar to the one above but without success. Can anyone please help (so r1 is identical to r2) in code below - the deepcopy fails so almost certain the function will fail too).

function reset_statsfuns_rng(rng_state)

StatsFuns.Random.GLOBAL_RNG.seed = rng_state.seed

StatsFuns.Random.GLOBAL_RNG.state = rng_state.state

StatsFuns.Random.GLOBAL_RNG.vals = rng_state.vals

StatsFuns.Random.GLOBAL_RNG.idx = rng_state.idx

end

rs = deepcopy(StatsFuns.Random.GLOBAL_RNG)

r1 = rand(Gamma(3,11),5)

reset_statsfuns_rng(rs)

r2 = rand(Gamma(3,11),5)

Thanks.