# Possible to change the global RNG to a custom RNG?

**URL:** <https://discourse.julialang.org/t/possible-to-change-the-global-rng-to-a-custom-rng/113008>\
**Category:** General Usage\
**Created:** [April 16, 2024, 12:36pm UTC](https://discourse.julialang.org/t/possible-to-change-the-global-rng-to-a-custom-rng/113008 "2024-04-16T12:36:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [April 16, 2024, 12:36pm UTC](https://discourse.julialang.org/t/possible-to-change-the-global-rng-to-a-custom-rng/113008/1 "2024-04-16T12:36:25Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [April 16, 2024, 12:39pm UTC](https://discourse.julialang.org/t/possible-to-change-the-global-rng-to-a-custom-rng/113008/2 "2024-04-16T12:39:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [April 16, 2024, 12:41pm UTC](https://discourse.julialang.org/t/possible-to-change-the-global-rng-to-a-custom-rng/113008/3 "2024-04-16T12:41:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [April 16, 2024, 2:09pm UTC](https://discourse.julialang.org/t/possible-to-change-the-global-rng-to-a-custom-rng/113008/4 "2024-04-16T14:09:14Z")

</div>

You could use singleton constructions, for example

```julia
module YourPackage
defaultRNG() = ...
end

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

```

Not sure if that is a crime though 😉

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [April 18, 2024, 5:38am UTC](https://discourse.julialang.org/t/possible-to-change-the-global-rng-to-a-custom-rng/113008/5 "2024-04-18T05:38:27Z")

</div>

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