Possible to change the global RNG to a custom RNG?

All my (BetaML) stochastic functions accept a RNG parameter, defaulting to Random.GLOBAL_RNG.

Now, in a certain project I am ok to trade off RN production efficiency with stability and use StableRNGs, but I would like to avoid passing a StableRNG object to all my functions.

Is there a way to set a StableRNG as the GLOBAL_RNG so that all my functions take that one in place of the non-guaranteed by Julia version default RNG ?

Something like this (that obviously doesn’t work):

project_rng = StableRNG(123)
Random.GLOBAL_RNG = project_rng # can't rebind the constant 

No, the default/task local RNG can’t be changed to a custom type.

Any workaround to avoid passing a custom RNG (a StableRNG) everywhere ?

You could use singleton constructions, for example

module YourPackage
defaultRNG() = ...
end

# users can overwrite
const myRNG = StableRNG()
YourPackage.defaultRNG() = myRNG

Not sure if that is a crime though :wink:

If you might want to change the “default” RNG on the fly then maybe ScopedValues.jl is a good solution for you.