# Random number generators

**URL:** <https://discourse.julialang.org/t/random-number-generators/47613>\
**Category:** Statistics\
**Created:** [October 1, 2020, 5:32pm UTC](https://discourse.julialang.org/t/random-number-generators/47613 "2020-10-01T17:32:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![caetanotc](https://avatars.discourse-cdn.com/v4/letter/c/ce73a5/32.png) [@caetanotc](https://discourse.julialang.org/u/caetanotc)\
**Post date:** [October 1, 2020, 5:32pm UTC](https://discourse.julialang.org/t/random-number-generators/47613/1 "2020-10-01T17:32:09Z")

</div>

Hi, I’m new to julia and have some questions about RNG’s.

If I understood well, MersenneTwister is the default method, but MersenneTwister objects are linked with the Random library and rand() function isn’t.

When using the rand() function without Random is there any limitation? Or it will still return [0,1) MersenneTwister random numbers.

Thanks 🙂

---

<div class="post-metadata">

**Author:** ![edwinb-ai](https://avatars.discourse-cdn.com/v4/letter/e/bb73d2/32.png) [@edwinb-ai](https://discourse.julialang.org/u/edwinb-ai)\
**Post date:** [October 1, 2020, 6:01pm UTC](https://discourse.julialang.org/t/random-number-generators/47613/2 "2020-10-01T18:01:25Z")

</div>

If you look at the documentation for [Random Numbers](https://docs.julialang.org/en/v1/stdlib/Random/), the `rand` function has the following arguments and types

```julia
rand([rng=GLOBAL_RNG], [S], [dims...])

```

with the first argument being an `AbstractRNG` object. This means that you can actually call the `rand` function on any other type that inherits this `AbstractRNG` type. By doing this, you can effectively create multiple other RNG implementations, such is the case of the [RandomNumbers.jl](https://sunoru.github.io/RandomNumbers.jl/stable/) package.

Now, if you were to just call `rand()` out of the box, a `GLOBAL_RNG` is created which essentially creates a `MersenneTwister` type and uses that as the RNG. Thus it will return a random number from a Mersenne Twister implementation, as you have pointed out.

Finally, although it seems that `rand` is in `Base` and is separated from the `Random` module, actually the `Random` module is the one that is providing the implementation for the `rand` function, so they are actually one and the same. My guess is that `rand` is in `Base` for convenience, as it is used very frequently.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 2, 2020, 11:37am UTC](https://discourse.julialang.org/t/random-number-generators/47613/3 "2020-10-02T11:37:20Z")

</div>

> [@edwinb-ai](#):
>
> My guess is that `rand` is in `Base` for convenience, as it is used very frequently.

cf

> <https://github.com/JuliaLang/julia/issues/35899>
>
> There are a number of cases where stdlibs type pirate base (or vice versa, depen…ding on how you look at it), which are grinding @KristofferC's gears, partly because it's just bad m'kay, but also because it means that you can't be sure whether a project doesn't depend on a stdlib just because it doesn't import it. One such example is \[here\](https://github.com/JuliaLang/julia/pull/35894) due to Base providing \`rand\` which is mostly defined in Random. Another famous example is \`LinearAlgebra\` defining \`\*\` on matrices and vectors. It's probably not possible to eliminate the these piracies in the 1.x timeframe, but we should keep track of them and try to figure out what to do about this for Julia 2.0 when we can actually break some things.

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [October 2, 2020, 1:28pm UTC](https://discourse.julialang.org/t/random-number-generators/47613/4 "2020-10-02T13:28:41Z")

</div>

> [@caetanotc](#):
>
> MersenneTwister is the default method […] will still return [0,1)

It’s still the default, I suggested another RNG, and my understanding is the default will be changed, but hasn’t been for Julia 1.6. I also do recall a change to return (0,1] to avoid a very unlikely division-by-zero later when using the resulting number. I’m not sure that change is yet in.
