How to save random number generator state?

question

#1

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?


#2

One possible idiom:

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

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

#3

Base.Random.GLOBAL_RNG.seed


#4

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).


#5

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