# How to change the default RNG for a given script?

**URL:** <https://discourse.julialang.org/t/how-to-change-the-default-rng-for-a-given-script/132481>\
**Category:** General Usage\
**Tags:** random, rng\
**Created:** [September 18, 2025, 9:04am UTC](https://discourse.julialang.org/t/how-to-change-the-default-rng-for-a-given-script/132481 "2025-09-18T09:04:28Z")\
**Posts on this page:** 8\
**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:** [September 18, 2025, 9:04am UTC](https://discourse.julialang.org/t/how-to-change-the-default-rng-for-a-given-script/132481/1 "2025-09-18T09:04:28Z")

</div>

I know I can pass RNG to many (all?) stochastic functions, and that `StableRNG` is slow, but is there a way I can set at the beginning of my model something like:

```julia-auto
random_seed = 123 # from a config file
Random.GLOBAL_RNG = StableRNG(123)`

```

?

This doesn’t work as Random.GLOBAL\_RNG is a constant.

---

<div class="post-metadata">

**Author:** ![wheeheee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wheeheee/32/36663_2.png) [@wheeheee](https://discourse.julialang.org/u/wheeheee)\
**Post date:** [September 18, 2025, 9:16am UTC](https://discourse.julialang.org/t/how-to-change-the-default-rng-for-a-given-script/132481/2 "2025-09-18T09:16:32Z")

</div>

No, unless you want to edit Random.jl, but you can `seed!(123)`, or you just have to pass the rng object down the call chain.

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [September 18, 2025, 2:35pm UTC](https://discourse.julialang.org/t/how-to-change-the-default-rng-for-a-given-script/132481/3 "2025-09-18T14:35:47Z")

</div>

And if it’s too tedious to pass down the call chain, you could make your RNG a global const and just access it at the call sites. Although if it’s not then passing it down is a tad nicer.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [September 18, 2025, 2:56pm UTC](https://discourse.julialang.org/t/how-to-change-the-default-rng-for-a-given-script/132481/4 "2025-09-18T14:56:21Z")

</div>

> [@mikmoore](#):
>
> then passing it down is a tad nicer.

And faster:

> **[The need for rand speed](https://bkamins.github.io/julialang/2020/11/20/rand.html)**
>
> Introduction

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [September 18, 2025, 11:22pm UTC](https://discourse.julialang.org/t/how-to-change-the-default-rng-for-a-given-script/132481/5 "2025-09-18T23:22:56Z")

</div>

One could use a ScopedValue for the best of both worlds. It would be nice if the default rng was a ScopedValue but that’s probably infeasible as the default rng is very special and gets seeded on task creation for reproducible randomness with multi threading.

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [September 19, 2025, 4:07am UTC](https://discourse.julialang.org/t/how-to-change-the-default-rng-for-a-given-script/132481/6 "2025-09-19T04:07:26Z")

</div>

I was curious about these statements:

> [@sylvaticus](#):
>
> and that `StableRNG` is slow

> [@giordano](#):
>
> > [@mikmoore](#):
> >
> > then passing it down is a tad nicer.
> 
> And faster:

So I tested on Julia v1.11.6

```julia-auto
@btime pi_default($N);
 2.505 s (0 allocations: 0 bytes)

@btime pi_global($N);
 2.524 s (0 allocations: 0 bytes)

@btime pi_local($N, $rng);
 2.511 s (0 allocations: 0 bytes)

@btime pi_local($N, $stablerng);
 1.394 s (0 allocations: 0 bytes)

```

> **Code**
>
> ```julia-auto
> using Random
> using StableRNGs
> using BenchmarkTools
> 
> const MY_RNG = Xoshiro()
> 
> function pi_default(n::Int)
> s = 0
> for _ in 1:n
> s += rand()^2 + rand()^2 < 1
> end
> return 4 * s / n
> end
> 
> function pi_global(n::Int)
> s = 0
> for _ in 1:n
> s += rand(MY_RNG)^2 + rand(MY_RNG)^2 < 1
> end
> return 4 * s / n
> end
> 
> function pi_local(n::Int, rng::AbstractRNG)
> s = 0
> for _ in 1:n
> s += rand(rng)^2 + rand(rng)^2 < 1
> end
> return 4 * s / n
> end
> 
> N = 10^9
> rng = Xoshiro()
> stablerng = StableRNG(123)
> 
> @btime pi_default($N);
> @btime pi_global($N);
> @btime pi_local($N, $rng);
> @btime pi_local($N, $stablerng);
> 
> ```

---

<div class="post-metadata">

**Author:** ![wheeheee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wheeheee/32/36663_2.png) [@wheeheee](https://discourse.julialang.org/u/wheeheee)\
**Post date:** [September 19, 2025, 2:12pm UTC](https://discourse.julialang.org/t/how-to-change-the-default-rng-for-a-given-script/132481/7 "2025-09-19T14:12:14Z")

</div>

Yes, for me `StableRNG`s are slightly faster in that case.  
However, the story is different when generating large-ish arrays of random numbers, as it seems the default rng has a good SIMD implementation for that case.

```julia-repl
julia> stab = StableRNG(123);

julia> @btime rand(N) setup=(N=10^5);
  49.200 μs (3 allocations: 781.32 KiB)

julia> @btime rand($stab, N) setup=(N=10^5);
  106.700 μs (3 allocations: 781.32 KiB)

julia> @btime rand(N) setup=(N=10^7);
  13.358 ms (3 allocations: 76.29 MiB)

julia> @btime rand($stab, N) setup=(N=10^7);
  18.210 ms (3 allocations: 76.29 MiB)

```

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [September 19, 2025, 2:28pm UTC](https://discourse.julialang.org/t/how-to-change-the-default-rng-for-a-given-script/132481/8 "2025-09-19T14:28:35Z")

</div>

Separate from the `Random` vs `StableRNGs` performance comparison:

One factor making RNGs slow when generating just a few random numbers in a call is that they’re mutable. Reading/writing the RNG state to RAM incurs some overhead for every `rand` call (not every number that’s generated, which is why only small calls suffer). An immutable RNG state (implemented like an iterator, for example, where the state is carried as a local variable) would not need to do this, so would be faster for small calls (orthogonal to the SIMD question).

It’s been on my list (long and probably never to be completed) to experiment with adding immutable RNGs to `Random`, but it’s a pretty big refactor to integrate them properly and there would be some debate over the interface.
