# Same random seed, but different random numbers?

**URL:** <https://discourse.julialang.org/t/same-random-seed-but-different-random-numbers/83731>\
**Category:** General Usage\
**Created:** [July 4, 2022, 8:57am UTC](https://discourse.julialang.org/t/same-random-seed-but-different-random-numbers/83731 "2022-07-04T08:57:13Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raymond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raymond/32/36557_2.png) [@Raymond](https://discourse.julialang.org/u/Raymond)\
**Post date:** [July 4, 2022, 8:57am UTC](https://discourse.julialang.org/t/same-random-seed-but-different-random-numbers/83731/1 "2022-07-04T08:57:14Z")

</div>

Dear all,

I try to generate random number using both `randn()` and `rand()`, I set the same random seed, but why they generate different results?

```julia
julia> using Distributions, Random

julia> Random.seed!(123)
TaskLocalRNG()

julia> a = rand(Normal(0, 1), 100);

julia> Random.seed!(123)
TaskLocalRNG()

julia> b = randn(100);

julia> a == b
false

```

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [July 4, 2022, 9:12am UTC](https://discourse.julialang.org/t/same-random-seed-but-different-random-numbers/83731/2 "2022-07-04T09:12:55Z")

</div>

It works for vectors up to 7 elements long for some reason.

```julia
julia> begin
         N = 7
         Random.seed!(123)
         a = rand(Normal(0, 1), N)
         Random.seed!(123)
         b = randn(N)
         a == b
       end
true

```

---

<div class="post-metadata">

**Author:** ![Raymond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raymond/32/36557_2.png) [@Raymond](https://discourse.julialang.org/u/Raymond)\
**Post date:** [July 4, 2022, 9:33am UTC](https://discourse.julialang.org/t/same-random-seed-but-different-random-numbers/83731/3 "2022-07-04T09:33:57Z")

</div>

Yes. It’s strange. When N\>7, the results are different.

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [July 4, 2022, 9:41am UTC](https://discourse.julialang.org/t/same-random-seed-but-different-random-numbers/83731/4 "2022-07-04T09:41:11Z")

</div>

It comes from 7 being hardcoded as a specialization here

```julia
function $randfun!(rng::Union{Xoshiro, TaskLocalRNG}, A::Array{Float64})
    if length(A) < 7
        for i in eachindex(A)
            @inbounds A[i] = $randfun(rng, Float64)
        end
    else
        GC.@preserve A rand!(rng, UnsafeView{UInt64}(pointer(A), length(A)))

        for i in eachindex(A)
            @inbounds A[i] = $_randfun(rng, reinterpret(UInt64, A[i]) >>> 12)
        end
    end
    A
end

```

though I’m not sure why `randn(N)` calls this while `rand(Normal(0, 1), N)` uses this

```julia
function _rand!(rng::AbstractRNG, sampler::Sampleable{Univariate}, A::AbstractArray{<:Real})
    for i in eachindex(A)
        @inbounds A[i] = rand(rng, sampler)
    end
    return A
end

```

But here we see that for `N<7` the implementations seems similar with one element being generated at a time, and if you actually generate a 100 numbers one at a time with the two strategies they are the same, while for `N>7` `randn` will generate stuff in blocks with some other strategy that results in different numbers.

---

<div class="post-metadata">

**Author:** ![Raymond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raymond/32/36557_2.png) [@Raymond](https://discourse.julialang.org/u/Raymond)\
**Post date:** [July 4, 2022, 9:54am UTC](https://discourse.julialang.org/t/same-random-seed-but-different-random-numbers/83731/5 "2022-07-04T09:54:09Z")

</div>

I hope that in Julia 1.8 release version, this problem can be solved.

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [July 4, 2022, 10:19am UTC](https://discourse.julialang.org/t/same-random-seed-but-different-random-numbers/83731/7 "2022-07-04T10:19:59Z")

</div>

> [@Raymond](#):
>
> I hope that in Julia 1.8 release version, this problem can be solved.

I am not sure this is a “problem”.  
`rand()` and `rand(Normal(0, 1)` are not guaranteed (indeed…) to use the same algorithm, so why should we assume they provide the same random streams ? It would be a problem if any of the two provides a different random stream with the same seed…

---

<div class="post-metadata">

**Author:** ![Raymond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raymond/32/36557_2.png) [@Raymond](https://discourse.julialang.org/u/Raymond)\
**Post date:** [July 4, 2022, 10:28am UTC](https://discourse.julialang.org/t/same-random-seed-but-different-random-numbers/83731/8 "2022-07-04T10:28:23Z")

</div>

But I think `randn()` and `rand(Normal(0, 1))` should get the same random numbers with the same seed.  
in Julia, both `randn()` and `rand(Normal(0, 1))` generate normally-distributed random number of with mean 0 and standard deviation 1.

---

<div class="post-metadata">

**Author:** ![Raymond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raymond/32/36557_2.png) [@Raymond](https://discourse.julialang.org/u/Raymond)\
**Post date:** [July 4, 2022, 10:32am UTC](https://discourse.julialang.org/t/same-random-seed-but-different-random-numbers/83731/9 "2022-07-04T10:32:05Z")

</div>

When we generate one random number with the same seed, we find that they always get the same random number.

```julia
julia> Random.seed!(123)
TaskLocalRNG()

julia> randn()
-0.6457306721039767

julia> Random.seed!(123)
TaskLocalRNG()

julia> rand(Normal(0, 1))
-0.6457306721039767

```

So I think they should also get the same random numbers when N \> 7.

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [July 4, 2022, 10:42am UTC](https://discourse.julialang.org/t/same-random-seed-but-different-random-numbers/83731/10 "2022-07-04T10:42:07Z")

</div>

I don’t feel the need to have the assumption that they do generate them in the same way (hence with the same random stream). After all, they are different functions.  
It happens that for N\<=7 they do produce the same numbers/consume the same random stream from the RNG, but nothing guarantees that this is a general rule (indeed it isn’t).  
We shouldn’t ever induce a general rule from a few observations, like [in the Bertrand Russell’s methaphor](https://mashimo.wordpress.com/2013/03/12/bertrand-russells-inductivist-turkey/).

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [July 4, 2022, 9:17pm UTC](https://discourse.julialang.org/t/same-random-seed-but-different-random-numbers/83731/11 "2022-07-04T21:17:38Z")

</div>

Think about it this way. `randn(N)` and `rand(Normal(0, 1), N)` are 2 different ways of sampling a standard normal variable; if you take enough samples (large N) from either, you see a normal distribution. They don’t promise to or need to do it in the same order or the same way to accomplish that.

Setting the random seed is intended for reproducing the pseudorandom sampling. If you’re trying to reproduce any behavior, you should run the exact same code. Never substitute any step, even if you’re sure they are equivalent.

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [July 5, 2022, 7:30am UTC](https://discourse.julialang.org/t/same-random-seed-but-different-random-numbers/83731/12 "2022-07-05T07:30:45Z")

</div>

By the same logic, you should be even more discontent with

```julia
julia> using Random

julia> Random.seed!(123)
TaskLocalRNG()

julia> a = randn(7);

julia> Random.seed!(123)
TaskLocalRNG()

julia> b = randn(8)[1:7];

julia> a == b
false

```

But as others have said, you cannot reasonably expect the same random stream if you are not running the exact same code. The fact that a specialization exists for `N>7` (or `N>13` for Mersenne Twisters) is a good sign. It means someone has given serious thought to maximizing performance, which is next to correctness the most important goal.

If any implementation is “at fault”, it’s `Distributions`’, because their sampler circumvents the specialization in `Random`.

In any case, I cannot imagine a scenario in which requiring `randn == rand(Normal)` is sensible.

---

<div class="post-metadata">

**Author:** ![Raymond](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raymond/32/36557_2.png) [@Raymond](https://discourse.julialang.org/u/Raymond)\
**Post date:** [July 5, 2022, 10:11am UTC](https://discourse.julialang.org/t/same-random-seed-but-different-random-numbers/83731/13 "2022-07-05T10:11:00Z")

</div>

Thanks for all of your answers. It help me a lot.
