# Randn isn't consistent with Seed?

**URL:** <https://discourse.julialang.org/t/randn-isnt-consistent-with-seed/68287>\
**Category:** General Usage\
**Tags:** random\
**Created:** [September 16, 2021, 9:08pm UTC](https://discourse.julialang.org/t/randn-isnt-consistent-with-seed/68287 "2021-09-16T21:08:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![klowrey](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klowrey/32/2266_2.png) [@klowrey](https://discourse.julialang.org/u/klowrey)\
**Post date:** [September 16, 2021, 9:08pm UTC](https://discourse.julialang.org/t/randn-isnt-consistent-with-seed/68287/1 "2021-09-16T21:08:41Z")

</div>

When I run the following few lines of code, I get the attached behavior where the first few elements of using `randn` will be the same but after a while (not sure if machine dependent) the same rng will diverge, but only with `randn` as `rand` works as expected. Just wanted to ask here if folks had an explanation or any idea why this occurs.

```julia
using Random
r1 = MersenneTwister(1)
r2 = MersenneTwister(1)

N = 20
a = [randn(r1) for i=1:N] # there seems to be a difference in batch calling randn vs sequentially
b = randn(r2, N)

julia> a-b
20-element Vector{Float64}:
  0.0
  0.0
  0.0
  0.0
  0.0
  0.0
  0.0
 -1.5094533110800028
  0.3009560212569654
  2.698507875223443
  0.05374271140203246
  0.5318500786152787
 -0.12491733251506171
 -1.4856083624596992
 -0.05039511672896463
  0.4718855274878431
 -1.102049482128638
 -1.7233820234743904
  0.8144196087413249
  1.0162002156875278

```

---

<div class="post-metadata">

**Author:** ![staticfloat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/staticfloat/32/34_2.png) [@staticfloat](https://discourse.julialang.org/u/staticfloat)\
**Post date:** [September 16, 2021, 9:11pm UTC](https://discourse.julialang.org/t/randn-isnt-consistent-with-seed/68287/2 "2021-09-16T21:11:15Z")

</div>

@rfourquet Pinging you as our native Random expert.

---

<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:** [September 16, 2021, 9:15pm UTC](https://discourse.julialang.org/t/randn-isnt-consistent-with-seed/68287/3 "2021-09-16T21:15:39Z")

</div>

I think this is from [https://github.com/JuliaLang/julia/pull/35078](https://github.com/JuliaLang/julia/pull/35078).

---

<div class="post-metadata">

**Author:** ![klowrey](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klowrey/32/2266_2.png) [@klowrey](https://discourse.julialang.org/u/klowrey)\
**Post date:** [September 17, 2021, 12:41am UTC](https://discourse.julialang.org/t/randn-isnt-consistent-with-seed/68287/4 "2021-09-17T00:41:38Z")

</div>

Yeah, that must be it. I suspected that it was some optimization for batch producing the randoms, but was really scratching my head. It seems a little bit breaking that they don’t produce the same sequence since N randn values should be the same with the same seed, but if people know about this then they can avoid it.

Thanks all!

---

<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:** [September 21, 2021, 2:29pm UTC](https://discourse.julialang.org/t/randn-isnt-consistent-with-seed/68287/5 "2021-09-21T14:29:07Z")

</div>

> [@klowrey](#):
>
> but only with `randn` as `rand` works as expected

Note that the same behavior exists also with `rand`, for a bigger `N`.

---

<div class="post-metadata">

**Author:** ![klowrey](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klowrey/32/2266_2.png) [@klowrey](https://discourse.julialang.org/u/klowrey)\
**Post date:** [September 21, 2021, 7:41pm UTC](https://discourse.julialang.org/t/randn-isnt-consistent-with-seed/68287/6 "2021-09-21T19:41:43Z")

</div>

Very good to know, thanks!

---

<div class="post-metadata">

**Author:** ![palday](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palday/32/12640_2.png) [@palday](https://discourse.julialang.org/u/palday)\
**Post date:** [September 24, 2021, 2:14pm UTC](https://discourse.julialang.org/t/randn-isnt-consistent-with-seed/68287/7 "2021-09-24T14:14:43Z")

</div>

See also [Weird PRNG behavior](https://discourse.julialang.org/t/weird-prng-behavior/63186)
