# How could I save and restore the status of random seed?

**URL:** <https://discourse.julialang.org/t/how-could-i-save-and-restore-the-status-of-random-seed/61941>\
**Category:** New to Julia\
**Created:** [May 27, 2021, 3:27pm UTC](https://discourse.julialang.org/t/how-could-i-save-and-restore-the-status-of-random-seed/61941 "2021-05-27T15:27:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [May 27, 2021, 3:27pm UTC](https://discourse.julialang.org/t/how-could-i-save-and-restore-the-status-of-random-seed/61941/1 "2021-05-27T15:27:40Z")

</div>

as per the following, how could I make sure the random seed remains unchanged before and after calling the function?

```julia
function f()
    rng0 = Random.default_rng() # save the current seed

    ### section for deterministic randomized algorithm
    Random.seed!(123)
    xxxxxx
    xxxxxxx
    ###

    # ??? how could I restore the seed to rng0 ???
end

```

thanks.

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [May 27, 2021, 4:34pm UTC](https://discourse.julialang.org/t/how-could-i-save-and-restore-the-status-of-random-seed/61941/2 "2021-05-27T16:34:58Z")

</div>

`Random.seed!(rng0.seed)` is one option.

Perhaps cleaner would be to leave the global random number generator alone:

```julia
function f()
  rng = MersenneTwister(123);
  x = rand(rng)
  [...]
end

```

---

<div class="post-metadata">

**Author:** ![cgeoga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cgeoga/32/216186_2.png) [@cgeoga](https://discourse.julialang.org/u/cgeoga)\
**Post date:** [May 27, 2021, 4:37pm UTC](https://discourse.julialang.org/t/how-could-i-save-and-restore-the-status-of-random-seed/61941/3 "2021-05-27T16:37:50Z")

</div>

Also, if you want determinism in your `rand`s to hold across Julia versions, you probably want to use something like `StableRNGs.jl` further suggesting that @hendri54’s suggestion is best here.

---

<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, 2021, 6:23am UTC](https://discourse.julialang.org/t/how-could-i-save-and-restore-the-status-of-random-seed/61941/4 "2021-05-28T06:23:28Z")

</div>

The `.seed` field of the global RNG will soon not exist anymore (in Julia 1.7). An alternative would be to copy the state:

```julia
julia> rand()
0.09028876420540066

julia> state = copy(Random.default_rng())
MersenneTwister(0xacbf17e36353b3a3a75764abc1e03e06, (0, 1002, 0, 2))

julia> rand()
0.5688115492566419

julia> copy!(Random.default_rng(), state)
MersenneTwister(0xacbf17e36353b3a3a75764abc1e03e06, (0, 1002, 0, 2))

julia> rand()
0.5688115492566419

```

This should also be quite faster than re-seeding.
