How to change the default random generator?
however, i prefer to use
rand() instead of
is it possible? this does not work
Random.seed!(Random.MersenneTwister(), 2); rand()
My recommendation is to pass
rng explicitly. However if you really want to have it implicit you could do:
const RNG = Random.MersenneTwister(1)
rand(args...) = Random.rand(RNG, args...)
seed! without passing an RNG in, and it will seed the default RNG:
Also, why do you want to use
MersenneTwister? It’s pretty much strictly worse than the new one.
Because I have old results that need to be reproducible.
I still think might be nice to be able to change the default RNG.
If you really want, you can redefine some methods, but I don’t recommend it:
julia> rng = MersenneTwister(0); Random.default_rng() = rng; rand()
julia> Random.seed!(0); rand()
Other methods might need to be redefined, see the file “stdlib/Random/src/RNGs.jl” for details. Also this will need more work if you want to be thread-safe.
There was a discussion about allowing changing the default RNG, if you are interested, at enable switching the default RNG (fix #23199) by rfourquet · Pull Request #23205 · JuliaLang/julia · GitHub.
This is unfortunately completely at odds with having the default RNG be fast, so it isn’t supported and likely won’t be. Consider: if the default RNG type is changeable, that would prevent inlining calls to it or specializing on the type in any way. So, if you need reproducibility of exact RNG streams, you’ll need to explicitly pass RNG arguments around.
Thank you very much! This is a very helpful discussion. I agree with your comments. I made changes in the code to explicitly pass RNG arguments.