# Dealing with seeds in random numbers

**URL:** https://discourse.julialang.org/t/dealing-with-seeds-in-random-numbers/29029
**Category:** New to Julia
**Created:** [September 21, 2019, 1:35pm UTC](https://discourse.julialang.org/t/dealing-with-seeds-in-random-numbers/29029 "2019-09-21T13:35:13Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![angeloaliano1](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@angeloaliano1](https://discourse.julialang.org/u/angeloaliano1)
#### Post date: [September 21, 2019, 1:35pm UTC](https://discourse.julialang.org/t/dealing-with-seeds-in-random-numbers/29029/1 "2019-09-21T13:35:13Z")

</div>

Dear users,  
Anyone could say me how can I generate random numbers in Julia by using a given seed?  
I accept examples.  
I need to generate some data for a paper, and then provide the seed for other researches to use the same ones.  
Thank you very much.

---

<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: [September 21, 2019, 1:51pm UTC](https://discourse.julialang.org/t/dealing-with-seeds-in-random-numbers/29029/2 "2019-09-21T13:51:25Z")

</div>

You do it by creating a “random number generator” (RNG) object (see the [`Random` docs](https://docs.julialang.org/en/latest/stdlib/Random/)):

```julia
julia> using Random

julia> rng = MersenneTwister(12345); # 12345 is the seed

julia> rand(rng, 10)

```

> I need to generate some data for a paper, and then provide the seed for other researches to use the same ones.

I would strongly urge you to **simply post the data**. (Many journals allow you to attach data files as “supplementary information”, and various fields also have public data repositories.)

As discussed e.g. [here](https://github.com/JuliaLang/julia/pull/30494), Julia is unlikely to guarantee indefinitely that pseudorandom streams always produce exactly the same sequence of numbers from a given seed. [Numpy recently came to the same conclusion](https://numpy.org/neps/nep-0019-rng-policy.html).

Of course, you could also supply the exact version of Julia to use and a [package manifest](https://docs.julialang.org/en/v1.0/stdlib/Pkg/) specifying the exact versions of all packages. Personally, I feel like this is the wrong way to approach reproducibility — decades from now, people should be able to reproduce your results without having to resurrect ancient languages and software versions (and maybe ancient hardware emulators!). The data files and algorithms should be archived in a manner that allows the results to be reproduced (at least up to small differences in rounding errors) with any language and set of libraries.

---

<div class="post-metadata">

### Author: ![angeloaliano1](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@angeloaliano1](https://discourse.julialang.org/u/angeloaliano1)
#### Post date: [September 21, 2019, 1:54pm UTC](https://discourse.julialang.org/t/dealing-with-seeds-in-random-numbers/29029/3 "2019-09-21T13:54:46Z")

</div>

Thank you so much for your detailed response!

---

<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: [September 21, 2019, 3:01pm UTC](https://discourse.julialang.org/t/dealing-with-seeds-in-random-numbers/29029/4 "2019-09-21T15:01:22Z")

</div>

> [@stevengj](#):
>
> As discussed e.g. [here](https://github.com/JuliaLang/julia/pull/30494), Julia is unlikely to guarantee indefinitely that pseudorandom streams always produce exactly the same sequence of numbers from a given seed. [Numpy recently came to the same conclusion](https://numpy.org/neps/nep-0019-rng-policy.html).

Do you think it would make sense to write up something similar (but shorter) in the manual as the protocol we follow for RNG changes? Eg

1. random streams are not guaranteed to be reproducible when the minor or major version of Julia changes,
2. but patch version changes should not affect the random stream,
3. old RNGs are kept available in some library.

---

<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: [September 21, 2019, 3:47pm UTC](https://discourse.julialang.org/t/dealing-with-seeds-in-random-numbers/29029/5 "2019-09-21T15:47:51Z")

</div>

> [@Tamas\_Papp](#):
>
> Do you think it would make sense to write up something similar (but shorter) in the manual as the protocol we follow for RNG changes?

> <https://github.com/JuliaLang/julia/pull/33350>
>
> See e.g. discussion in #30494 and \[on discourse\](https://discourse.julialang.org…/t/dealing-with-seeds-in-random-numbers/29029).
> 
> See also https://numpy.org/neps/nep-0019-rng-policy.html for a similar discussion by the NumPy developers.
