How to save random number generator state?

Suppose I have a loop where I generate samples with a stochastic function:

srand(2017)

for i=1:1000
  sample = stochastic_function(deterministic_parameters...)
end

I need to save the state in order to be able to do some calculations later. That is, I need to be able to pass in the same deterministic parameters and get the same exact sample. What is the appropriate idiom in Julia for achieving this goal?

One possible idiom:

for i=1:1000
  srand(i)
  stochastic_function(deterministic_parameters...)

  # save parameters and the seed
  save(deterministic_parameters, seed)
end

Base.Random.GLOBAL_RNG.seed

I don’t really understand what you need, but there is copy for MersenneTwister. For example r = copy(GLOBAL_RNG); rand ()...; copy!(GLOBAL_RNG, r).

2 Likes

Thank you all, I am just saving the setting the seed inside of the loop and saving it for now.