Store Random Number Generator outside Rand()

How can store an rng w/ seed outside of rand()?

julia> using Random, Distributions;

julia> rand(MersenneTwister(0), Normal())
0.6791074260357777

julia> rand(MersenneTwister(0), Normal())
0.6791074260357777

julia> rs=MersenneTwister(0)
MersenneTwister(0)

julia> rand(rs, Normal())
0.6791074260357777

julia> rand(rs, Normal())
0.8284134829000359

Since rs=MersenneTwister(0) I’d expect rand(rs, Normal()) to return the same thing each time…

rand modifies the RNG you pass to it (it’s a function with sideeffects, by the nature of how pseudo-RNGs work). If you want to keep getting the same value, reseed the rng object explicitly via seed!.

(BTW, in 1.7 there will be a new RNG called Xoroshiro, which will be smaller & faster. This will also be the new default RNG. In combination with some other changes to how RNGs work, like one default-RNG per task, the stream of random numbers generated by rand will change.)

1 Like