# Threads and stdlib Random

**URL:** <https://discourse.julialang.org/t/threads-and-stdlib-random/57539>\
**Category:** General Usage\
**Created:** [March 19, 2021, 1:55pm UTC](https://discourse.julialang.org/t/threads-and-stdlib-random/57539 "2021-03-19T13:55:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![evanfields](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evanfields/32/1744_2.png) [@evanfields](https://discourse.julialang.org/u/evanfields)\
**Post date:** [March 19, 2021, 1:55pm UTC](https://discourse.julialang.org/t/threads-and-stdlib-random/57539/1 "2021-03-19T13:55:02Z")

</div>

What’s the state of the Random stdlib and thread safety? I thought I remembered some notes on this from several Julia releases back, but the Random [docs](https://docs.julialang.org/en/v1/stdlib/Random/) have almost nothing on the topic. The only reference to threads is for `seed!`:

> If `rng` is not specified, it defaults to seeding the state of the shared thread-local generator.

Is `rand(rng)` threadsafe? What about `rand(Random.GLOBAL_RNG)`? What _is_ `Random.GLOBAL_RNG`, since it’s not a `MersenneTwister`? Thanks!

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [March 19, 2021, 2:07pm UTC](https://discourse.julialang.org/t/threads-and-stdlib-random/57539/2 "2021-03-19T14:07:47Z")

</div>

> [@evanfields](#):
>
> Is `rand(rng)` threadsafe?

no. basically you should think that when you do `rand()` in thread, each thread has its own `rng`. Which makes `rand()` thread-safe.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 19, 2021, 2:57pm UTC](https://discourse.julialang.org/t/threads-and-stdlib-random/57539/3 "2021-03-19T14:57:38Z")

</div>

> [@evanfields](#):
>
> What _is_ `Random.GLOBAL_RNG` , since it’s not a `MersenneTwister` ? Thanks!

It’s a special object (of singleton type `Random._GLOBAL_RNG`) that turns into a per-thread `MersenneTwister` by [calling `Random.default_rng()`](https://github.com/JuliaLang/julia/blob/efad4e3134540c7a45d385b047f145cb0da6d385/stdlib/Random/src/RNGs.jl#L388-L424).

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 19, 2021, 3:11pm UTC](https://discourse.julialang.org/t/threads-and-stdlib-random/57539/4 "2021-03-19T15:11:37Z")

</div>

> [@evanfields](#):
>
> the Random [docs](https://docs.julialang.org/en/v1/stdlib/Random/) have almost nothing on the topic.

I made a PR to add some basic info: [document thread safety for RNGs by stevengj · Pull Request #40109 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/40109)

---

<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:** [March 19, 2021, 4:27pm UTC](https://discourse.julialang.org/t/threads-and-stdlib-random/57539/5 "2021-03-19T16:27:25Z")

</div>

This works as well, I like I can choose to have a (a) random script, (b) deterministic script with individually random functions (same run of the script/model, same output) or (c) deterministic functions (same call with same input, same output), but it requires Julia \>= 1.5:

> [@Multithreaded random: different behaviour in Julia \>= 1.5 (not just different stream)](https://discourse.julialang.org/t/multithreaded-random-different-behaviour-in-julia-1-5-not-just-different-stream/57291):
>
> Hi, I just realised that my solution below for multithreaded random number generators works only in Julia \>= 1.5. In particular the test of “CASE A” fails, i.e. the two repetitions issue the same number. Is there a way to get the same framework working on Julia \<= 1.4 ? using Random, Test, Future, Statistics FIXEDRNG = MersenneTwister(123) function generateParallelRngs(rng::AbstractRNG, n::Integer) step = rand(rng,big(10)^20:big(10)^40) # making the step random too ! rngs = Vector{U…
