# Julia 1.7 saving and restoring RNG state

**URL:** <https://discourse.julialang.org/t/julia-1-7-saving-and-restoring-rng-state/73037>\
**Category:** General Usage\
**Tags:** threads, random\
**Created:** [December 13, 2021, 5:39pm UTC](https://discourse.julialang.org/t/julia-1-7-saving-and-restoring-rng-state/73037 "2021-12-13T17:39:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![crinders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crinders/32/8434_2.png) [@crinders](https://discourse.julialang.org/u/crinders)\
**Post date:** [December 13, 2021, 5:39pm UTC](https://discourse.julialang.org/t/julia-1-7-saving-and-restoring-rng-state/73037/1 "2021-12-13T17:39:36Z")

</div>

Previously I checkpointed the RNG in a Julia session using multiple threads (40) with

```julia
using JLD2
using Random
jldopen(Filename,'w') do F
   F["RNG"] = Random.default_rng()`
end

```

and restored with

```julia
using JLD2
using Random
myRNG = jldopen(File,"r") do F
   F["RNG"]
end
copy!(Random.default_rng(), myRNG)

```

The new PRNG paradigm breaks that pattern and I can’t find a quick fix.

```julia
julia> copy!(Random.default_rng(), myRNG)
ERROR: MethodError: no method matching copy!(::TaskLocalRNG, ::TaskLocalRNG)
Closest candidates are:
  copy!(::TaskLocalRNG, ::Xoshiro) at [...]/usr/share/julia/stdlib/v1.7/Random/src/Xoshiro.jl:154
  copy!(::Xoshiro, ::TaskLocalRNG) at [...]/usr/share/julia/stdlib/v1.7/Random/src/Xoshiro.jl:160
Stacktrace:
 [1] top-level scope
   @ REPL[6]:1

```

Any ideas?

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [December 13, 2021, 5:49pm UTC](https://discourse.julialang.org/t/julia-1-7-saving-and-restoring-rng-state/73037/2 "2021-12-13T17:49:36Z")

</div>

I’m not sure if it’s easy to do (or possible?), because each thread now has its own seed (you likely would need to suspend them all to do this, for some worst-case scenario, is it possible?). If you know you do not threading then something is likely possible. Or even with threading you can always opt into using the old MersenneTwister RNG (and lose out on threaded RNG and its speedup) and then your old method should be workable (and the RNG is only global). You must be sure all your code is changed to use the old RNG, and not some place left out using the new one (you can use both, that’s not the problem, only for what you’re trying to do).

```julia
julia> using Random
help?> MersenneTwister

```

---

<div class="post-metadata">

**Author:** ![crinders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crinders/32/8434_2.png) [@crinders](https://discourse.julialang.org/u/crinders)\
**Post date:** [December 13, 2021, 8:24pm UTC](https://discourse.julialang.org/t/julia-1-7-saving-and-restoring-rng-state/73037/3 "2021-12-13T20:24:57Z")

</div>

I could run have every thread store its PRNG and then load them back up, but that doesn’t resolve the issue with `copy!` (I’ve updated the OP to clarify).

---

<div class="post-metadata">

**Author:** ![crinders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crinders/32/8434_2.png) [@crinders](https://discourse.julialang.org/u/crinders)\
**Post date:** [December 13, 2021, 9:09pm UTC](https://discourse.julialang.org/t/julia-1-7-saving-and-restoring-rng-state/73037/4 "2021-12-13T21:09:38Z")

</div>

After reading up on things, it is my understanding that each `Task` inherits a PRNG from its parent and subsequently modifies the parent. I’m saving and re-entering on the top task of my problem, so I think it is safe to do

```julia
using JLD2
using Random
myRNG = jldopen(File,"r") do F
   F["RNG"]
end
S = Xoshiro()
copy!(S, myRNG)
copy!(default_rng(), S)

```

This solution doesn’t throw any errors as of yet, but I’m also not sure whether it has the intended feature of “reproducibility”.
