# Why is this line like this in Base.Random?

**URL:** <https://discourse.julialang.org/t/why-is-this-line-like-this-in-base-random/36626>\
**Category:** New to Julia\
**Created:** [March 28, 2020, 4:19am UTC](https://discourse.julialang.org/t/why-is-this-line-like-this-in-base-random/36626 "2020-03-28T04:19:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![xzackli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xzackli/32/38301_2.png) [@xzackli](https://discourse.julialang.org/u/xzackli)\
**Post date:** [March 28, 2020, 4:19am UTC](https://discourse.julialang.org/t/why-is-this-line-like-this-in-base-random/36626/1 "2020-03-28T04:19:15Z")

</div>

The Base.Random implementation of per-thread global RNGs seems to be a pattern for thread-safe mutations in Julia. I’m trying to implement something similar for a problem that strongly benefits from caching intermediate results for future calls to use.

However, I don’t understand the reasoning behind the one line in the ` __init__ ()` function. Here’s the Base.Random.RNGs [source](https://github.com/JuliaLang/julia/blob/712731e1a1e9e85a272343ba2e773707ca90a466/stdlib/Random/src/RNGs.jl#L294) from master,

```julia
const THREAD_RNGs = MersenneTwister[]
@inline default_rng() = default_rng(Threads.threadid())
@noinline function default_rng(tid::Int)
    0 < tid <= length(THREAD_RNGs) || _rng_length_assert()
    if @inbounds isassigned(THREAD_RNGs, tid)
        @inbounds MT = THREAD_RNGs[tid]
    else
        MT = MersenneTwister()
        @inbounds THREAD_RNGs[tid] = MT
    end
    return MT
end
@noinline _rng_length_assert() = @assert false "0 < tid <= length(THREAD_RNGs)"

function __init__ ()
    resize!(empty!(THREAD_RNGs), Threads.nthreads()) # ensures that we didn't save a bad object
end

```

My naive approach would have been to write an ` __init__ ()` which creates `nthreads()` copies of `MersenneTwister()`. Why is it preferable to fill an array with undef?

---

<div class="post-metadata">

**Author:** ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)\
**Post date:** [March 28, 2020, 6:00am UTC](https://discourse.julialang.org/t/why-is-this-line-like-this-in-base-random/36626/2 "2020-03-28T06:00:10Z")

</div>

In my understanding, it’s to reduce load time. Creating a `MersennTwister` is not free, so creating `nthreads()` copies of it unconditionally even when they are not needed is a waste of resources. One goal is to make julia startup as fast as possible.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [March 28, 2020, 7:54am UTC](https://discourse.julialang.org/t/why-is-this-line-like-this-in-base-random/36626/3 "2020-03-28T07:54:07Z")

</div>

Creating all the RNG on the same thread was measurably slower due to false sharing.

---

<div class="post-metadata">

**Author:** ![xzackli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xzackli/32/38301_2.png) [@xzackli](https://discourse.julialang.org/u/xzackli)\
**Post date:** [March 28, 2020, 3:09pm UTC](https://discourse.julialang.org/t/why-is-this-line-like-this-in-base-random/36626/4 "2020-03-28T15:09:11Z")

</div>

Thank you, that makes a lot of sense.
