# Reproducibility of multiple calls to rand

**URL:** <https://discourse.julialang.org/t/reproducibility-of-multiple-calls-to-rand/40316>\
**Category:** New to Julia\
**Tags:** reproducibility\
**Created:** [May 28, 2020, 4:24am UTC](https://discourse.julialang.org/t/reproducibility-of-multiple-calls-to-rand/40316 "2020-05-28T04:24:52Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![patrickpch](https://avatars.discourse-cdn.com/v4/letter/p/8edcca/32.png) [@patrickpch](https://discourse.julialang.org/u/patrickpch)\
**Post date:** [May 28, 2020, 4:24am UTC](https://discourse.julialang.org/t/reproducibility-of-multiple-calls-to-rand/40316/1 "2020-05-28T04:24:52Z")

</div>

Hello, new Julia (version 1.4.1) user here, trying to understand the reproducibility of random number sequences. Below is an example consisting of two tests:

```
using Random

blksize = 1000

#=
Test 1: Generate a sequence of 3000 random numbers in three blocks of
1000 and output the first number in every block, which should correspond
to elements 1, 1001, 2001 in the sequence.
=#
println("Test 1: Generate random numbers in blocks")

rng = MersenneTwister(1234)
blk1 = rand(rng, blksize)
println("blk1: ", blk1[1])
blk2 = rand(rng, blksize)
println("blk2: ", blk2[1])
blk3 = rand(rng, blksize)
println("blk3: ", blk3[1])

println("")

#=
Test 2: Generate all 3000 random numbers in a single call, and output elements 1,
1001, 2001 in the sequence.
=#
println("Test 2: Generate random numbers in a single big block")

rng = MersenneTwister(1234)
bigblk = rand(rng, 3*blksize)
println("blk1: ", bigblk[1])
println("blk2: ", bigblk[blksize + 1])
println("blk3: ", bigblk[2*blksize + 1])

```

Both sequences are generated from the same initial seed so I would expect the same results. But that’s not what I see:

```
Test 1: Generate random numbers in blocks
blk1: 0.5908446386657102
blk2: 0.0987939295091631
blk3: 0.2690250724723906

Test 2: Generate random numbers in a single big block
blk1: 0.5908446386657102
blk2: 0.9540122269417939
blk3: 0.0987939295091631

```

I am especially puzzled why the first element of blk2 in the first test would appear as the first element of blk3 in the second test. Also, I only see this behavior for block sizes \>400 or so, for smaller blocks I get the same results for both tests.

Any advice would be very much appreciated. Thanks!

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [May 28, 2020, 6:05am UTC](https://discourse.julialang.org/t/reproducibility-of-multiple-calls-to-rand/40316/2 "2020-05-28T06:05:37Z")

</div>

> [@patrickpch](#):
>
> Also, I only see this behavior for block sizes \>400 or so

Is it

```julia
julia> using Random

julia> Random.dsfmt_get_min_array_size()
382

```

?

Basically, the random number generator can only produce large numbers of random numbers at a time.  
So, for smaller numbers of random numbers, it fills a buffer and then can index into that one at a time.  
For larger numbers, it can fill arrays directly.

---

<div class="post-metadata">

**Author:** ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)\
**Post date:** [May 28, 2020, 6:07am UTC](https://discourse.julialang.org/t/reproducibility-of-multiple-calls-to-rand/40316/3 "2020-05-28T06:07:01Z")

</div>

The following line seems to be the reason why `rand(rng,n)` with `n <= 383` is handled differently from `n >= 384`:

> <https://github.com/JuliaLang/julia/blob/adf6d521afd125f2de12a3209b342148d78981c2/stdlib/Random/src/RNGs.jl#L486>

I am not sure whether Julia is supposed to guarantee that `[rand(n1); rand(n2)]` is the same as `rand(n1+n2)`. If so, your findings would indicate a bug.

---

<div class="post-metadata">

**Author:** ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)\
**Post date:** [May 28, 2020, 6:07am UTC](https://discourse.julialang.org/t/reproducibility-of-multiple-calls-to-rand/40316/4 "2020-05-28T06:07:27Z")

</div>

Yes, precisely.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [May 28, 2020, 8:13am UTC](https://discourse.julialang.org/t/reproducibility-of-multiple-calls-to-rand/40316/5 "2020-05-28T08:13:11Z")

</div>

> [@ettersi](#):
>
> I am not sure whether Julia is supposed to guarantee that `[rand(n1); rand(n2)]` is the same as `rand(n1+n2)` . If so, your findings would indicate a bug.

I don’t think there is any such guarantee.

---

<div class="post-metadata">

**Author:** ![lobingera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lobingera/32/211_2.png) [@lobingera](https://discourse.julialang.org/u/lobingera)\
**Post date:** [May 28, 2020, 4:19pm UTC](https://discourse.julialang.org/t/reproducibility-of-multiple-calls-to-rand/40316/6 "2020-05-28T16:19:48Z")

</div>

“By using an RNG parameter initialized with a given seed, you can reproduce the same pseudorandom number sequence when running your program multiple times.” - doesn’t talk about how you read the sequence.

---

<div class="post-metadata">

**Author:** ![patrickpch](https://avatars.discourse-cdn.com/v4/letter/p/8edcca/32.png) [@patrickpch](https://discourse.julialang.org/u/patrickpch)\
**Post date:** [May 28, 2020, 4:50pm UTC](https://discourse.julialang.org/t/reproducibility-of-multiple-calls-to-rand/40316/7 "2020-05-28T16:50:18Z")

</div>

I see…that is unfortunate, it would make testing a little easier if a given seed could yield a reproducible sequence regardless of how rand is called. Thanks everyone for your help!

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [May 28, 2020, 4:57pm UTC](https://discourse.julialang.org/t/reproducibility-of-multiple-calls-to-rand/40316/8 "2020-05-28T16:57:28Z")

</div>

You may be interested in [StableRNGs.jl](https://github.com/rfourquet/StableRNGs.jl) — it’s a simple RNG geared for usage in tests (as the builtin RNG may change/improve in future versions of Julia), but it’s also a _really simple and straightforward_ RNG that I think will happen to have the perfectly-sequential property you’re after.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [May 28, 2020, 6:13pm UTC](https://discourse.julialang.org/t/reproducibility-of-multiple-calls-to-rand/40316/9 "2020-05-28T18:13:38Z")

</div>

> [@lobingera](#):
>
> doesn’t talk about how you read the sequence

Yeah, exactly. So it is then not guaranteed.

---

<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:** [May 28, 2020, 10:21pm UTC](https://discourse.julialang.org/t/reproducibility-of-multiple-calls-to-rand/40316/10 "2020-05-28T22:21:22Z")

</div>

> [@mbauman](#):
>
> RNG that I think will happen to have the perfectly-sequential property you’re after.

Yes it’s indeed “guaranteed” 🙂

---

<div class="post-metadata">

**Author:** ![patrickpch](https://avatars.discourse-cdn.com/v4/letter/p/8edcca/32.png) [@patrickpch](https://discourse.julialang.org/u/patrickpch)\
**Post date:** [May 28, 2020, 11:10pm UTC](https://discourse.julialang.org/t/reproducibility-of-multiple-calls-to-rand/40316/11 "2020-05-28T23:10:27Z")

</div>

This is exactly what I need…thanks!
