# How should I understand the scope of Random.seed?

**URL:** <https://discourse.julialang.org/t/how-should-i-understand-the-scope-of-random-seed/102894>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [August 17, 2023, 7:37am UTC](https://discourse.julialang.org/t/how-should-i-understand-the-scope-of-random-seed/102894 "2023-08-17T07:37:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zerosum](https://avatars.discourse-cdn.com/v4/letter/z/b2d939/32.png) [@Zerosum](https://discourse.julialang.org/u/Zerosum)\
**Post date:** [August 17, 2023, 7:37am UTC](https://discourse.julialang.org/t/how-should-i-understand-the-scope-of-random-seed/102894/1 "2023-08-17T07:37:19Z")

</div>

```julia
function testing()

    function gen_a()
        return rand()
    end

    function gen_b()
        Random.seed!(111)

        return rand()
    end

    return gen_a() + gen_b()
end

testing()

result : 0.8538390963198838

```

Everytime I get the same value. My understanding was that the Random.seed! should fix only the RNG inside gen\_b(). How can I set it locally only inside gen\_b() function?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [August 17, 2023, 7:46am UTC](https://discourse.julialang.org/t/how-should-i-understand-the-scope-of-random-seed/102894/2 "2023-08-17T07:46:15Z")

</div>

Well they both use the same global one (thread-local) by default, so you’ll get the same value after the second invocation, because then `gen_a()` will always do the same second value in the rng seeded by b the run before.

You can create a separate random number generator for b instead and pass that to `rand()`, have a look at this page in the docs [Random Numbers · The Julia Language](https://docs.julialang.org/en/v1/stdlib/Random/)

Looks something like this example on that page

```julia
julia> rng = MersenneTwister(1234);

julia> randexp(rng, Float32)
2.4835055f0

```

---

<div class="post-metadata">

**Author:** ![Zerosum](https://avatars.discourse-cdn.com/v4/letter/z/b2d939/32.png) [@Zerosum](https://discourse.julialang.org/u/Zerosum)\
**Post date:** [August 17, 2023, 7:52am UTC](https://discourse.julialang.org/t/how-should-i-understand-the-scope-of-random-seed/102894/3 "2023-08-17T07:52:13Z")

</div>

> [@jules](#):
>
> `MersenneTwister(1234)`

Thank you very much

```julia
function testing()

    function gen_a()
        return rand()
    end

    function gen_b()
        a = MersenneTwister(1234);

        return rand(a)
    end

    return gen_a() + gen_b()
end

testing()

```

This works out as expected !

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [August 17, 2023, 8:32am UTC](https://discourse.julialang.org/t/how-should-i-understand-the-scope-of-random-seed/102894/4 "2023-08-17T08:32:12Z")

</div>

If you want fast RNG, use `Xoshiro` (also from the `Random` stdlib) instead of `MersenneTwister`. It’s also the type of the default RNG.
