# Store Random Number Generator outside Rand()

**URL:** <https://discourse.julialang.org/t/store-random-number-generator-outside-rand/67517>\
**Category:** Statistics\
**Tags:** random, rng\
**Created:** [September 1, 2021, 4:26pm UTC](https://discourse.julialang.org/t/store-random-number-generator-outside-rand/67517 "2021-09-01T16:26:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)\
**Post date:** [September 1, 2021, 4:26pm UTC](https://discourse.julialang.org/t/store-random-number-generator-outside-rand/67517/1 "2021-09-01T16:26:34Z")

</div>

How can store an `rng` w/ seed outside of `rand()`?

```julia
julia> using Random, Distributions;

julia> rand(MersenneTwister(0), Normal())
0.6791074260357777

julia> rand(MersenneTwister(0), Normal())
0.6791074260357777

julia> rs=MersenneTwister(0)
MersenneTwister(0)

julia> rand(rs, Normal())
0.6791074260357777

julia> rand(rs, Normal())
0.8284134829000359

```

Since `rs=MersenneTwister(0)` I’d expect `rand(rs, Normal())` to return the same thing each time…

---

<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:** [September 1, 2021, 4:31pm UTC](https://discourse.julialang.org/t/store-random-number-generator-outside-rand/67517/2 "2021-09-01T16:31:26Z")

</div>

`rand` modifies the RNG you pass to it (it’s a function with sideeffects, by the nature of how pseudo-RNGs work). If you want to keep getting the same value, reseed the rng object explicitly via `seed!`.

(BTW, in 1.7 there will be a new RNG called `Xoroshiro`, which will be smaller & faster. This will also be the new default RNG. In combination with some other changes to how RNGs work, like one default-RNG per task, the stream of random numbers generated by `rand` will change.)
