# Saving Random Number State for StatsFuns' distributions Gamma Beta

**URL:** <https://discourse.julialang.org/t/saving-random-number-state-for-statsfuns-distributions-gamma-beta/6504>\
**Category:** New to Julia\
**Created:** [October 17, 2017, 1:03pm UTC](https://discourse.julialang.org/t/saving-random-number-state-for-statsfuns-distributions-gamma-beta/6504 "2017-10-17T13:03:26Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![SteveStruggle](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@SteveStruggle](https://discourse.julialang.org/u/SteveStruggle)\
**Post date:** [October 17, 2017, 1:03pm UTC](https://discourse.julialang.org/t/saving-random-number-state-for-statsfuns-distributions-gamma-beta/6504/1 "2017-10-17T13:03:27Z")

</div>

On Stackoverflow there is a post showing how to save the current state of the base random number generator, so that one can go back and produce the same set of random numbers from a set point that is already so many draws in (ie it is not a simple set the seed response).

function reset\_global\_rng(rng\_state)  
Base.Random.GLOBAL\_RNG.seed = rng\_state.seed  
Base.Random.GLOBAL\_RNG.state = rng\_state.state  
Base.Random.GLOBAL\_RNG.vals = rng\_state.vals  
Base.Random.GLOBAL\_RNG.idx = rng\_state.idx  
end

using Distributions  
rs = deepcopy(Base.Random.GLOBAL\_RNG)  
println(rand(5))  
reset\_global\_rng(rs)  
println(rand(5))

This does not work for distributions such as Beta and Gamma which utilise the StatsFuns package random number generator instead of the Base one.  
I’ve tried to write a function similar to the one above but without success. Can anyone please help (so r1 is identical to r2) in code below - the deepcopy fails so almost certain the function will fail too).

function reset\_statsfuns\_rng(rng\_state)  
StatsFuns.Random.GLOBAL\_RNG.seed = rng\_state.seed  
StatsFuns.Random.GLOBAL\_RNG.state = rng\_state.state  
StatsFuns.Random.GLOBAL\_RNG.vals = rng\_state.vals  
StatsFuns.Random.GLOBAL\_RNG.idx = rng\_state.idx  
end

rs = deepcopy(StatsFuns.Random.GLOBAL\_RNG)  
r1 = rand(Gamma(3,11),5)  
reset\_statsfuns\_rng(rs)  
r2 = rand(Gamma(3,11),5)

Thanks.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 17, 2017, 1:10pm UTC](https://discourse.julialang.org/t/saving-random-number-state-for-statsfuns-distributions-gamma-beta/6504/2 "2017-10-17T13:10:29Z")

</div>

1. Please format your code using backticks (```).

2. Are you using this library?

> **[GitHub - JuliaStats/StatsFuns.jl: Mathematical functions related to statistics.](https://github.com/JuliaStats/StatsFuns.jl)**
>
> Mathematical functions related to statistics. Contribute to JuliaStats/StatsFuns.jl development by creating an account on GitHub.

AFAICT it does not use random numbers.

1. I would recommend using

> **[GitHub - JuliaStats/Distributions.jl: A Julia package for probability...](https://github.com/JuliaStats/Distributions.jl)**
>
> A Julia package for probability distributions and associated functions. - GitHub - JuliaStats/Distributions.jl: A Julia package for probability distributions and associated functions.

which AFAIK uses the global rng.

---

<div class="post-metadata">

**Author:** ![SteveStruggle](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@SteveStruggle](https://discourse.julialang.org/u/SteveStruggle)\
**Post date:** [October 17, 2017, 1:25pm UTC](https://discourse.julialang.org/t/saving-random-number-state-for-statsfuns-distributions-gamma-beta/6504/3 "2017-10-17T13:25:09Z")

</div>

Thanks for the backticks tip. The Distributions package calls the StatsFuns package for generating certain distributions (not uniform or normal). You can see this from the code source under the distributions for Gamma and Beta on the Distributions docs. So my question (unfortunately) still stands.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 17, 2017, 1:28pm UTC](https://discourse.julialang.org/t/saving-random-number-state-for-statsfuns-distributions-gamma-beta/6504/4 "2017-10-17T13:28:13Z")

</div>

AFAICT `Distributions.jl` uses the global RNG.

Also, please produce a minimal working example that at least runs and demonstrates your problem. The above code does not run, since there is no such thing as `StatsFuns.Random.GLOBAL_RNG`.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [October 17, 2017, 1:35pm UTC](https://discourse.julialang.org/t/saving-random-number-state-for-statsfuns-distributions-gamma-beta/6504/5 "2017-10-17T13:35:40Z")

</div>

GitHub allows you to capture line numbers in source code as links so tu ou can post a direct link to the line you are talking about. Otherwise it’s not clear what you mean.

---

<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:** [October 17, 2017, 1:46pm UTC](https://discourse.julialang.org/t/saving-random-number-state-for-statsfuns-distributions-gamma-beta/6504/6 "2017-10-17T13:46:51Z")

</div>

As an aside note, for `MersenneTwister` it would be cleaner/more future-proof to use `copy!/copy` rather than copying the fields manually; the internals will change eventually. E.g. `rng_state = copy(Base.Random.GLOBAL_RNG); ...; copy!(Base.Random.GLOBAL_RNG, rng_state)`.

---

<div class="post-metadata">

**Author:** ![SteveStruggle](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@SteveStruggle](https://discourse.julialang.org/u/SteveStruggle)\
**Post date:** [October 17, 2017, 2:26pm UTC](https://discourse.julialang.org/t/saving-random-number-state-for-statsfuns-distributions-gamma-beta/6504/7 "2017-10-17T14:26:09Z")

</div>

Code below hopefully demonstrates what I want to do. It works perfectly as is for the normal distribution. It does not work correctly for the gamma distribution (as shown in very bottom code) - reasons given in first message. If could someone could provide actual solution code I’d be very grateful.

```julia
using Distributions

function reset_global_rng(rng_state)
    Base.Random.GLOBAL_RNG.seed = rng_state.seed
    Base.Random.GLOBAL_RNG.state = rng_state.state
    Base.Random.GLOBAL_RNG.vals = rng_state.vals
    Base.Random.GLOBAL_RNG.idx = rng_state.idx
end

srand(1234) # set seed
# Now generate 100,000 Normal rands
r = rand(Normal(3,11),10000) 
#Now from herein my model can take two different paths:store generator
rs = deepcopy(Base.Random.GLOBAL_RNG)
# Now take path 1
path1 = rand(Normal(5,10),2)
# path2 has higher mean but more uncertainty but want to minimise
#monte carlo sampling error, hence desire to reset generator
reset_global_rng(rs)
path2 = rand(Normal(6,12),2)
#To prove the resetting worked do path3 below with same parameters as path1
reset_global_rng(rs)
path3 = rand(Normal(5,10),2)
println(path1==path3) # true

```

Below does not work correctly for gamma (or beta)

```julia
srand(1234) # set seed
# Now generate 100,000 Normal rands
r = rand(Normal(3,11),10000) 
#Now from herein my model can take two different paths and I want to traverse both
# store generator value
rs = deepcopy(Base.Random.GLOBAL_RNG)
# Now take path 1
path1 = rand(Gamma(5,10),2)
# path2 has higher mean but more uncertainty but want to minimise
#monte carlo sampling error, hence desire to reset generator
reset_global_rng(rs)
path2 = rand(Gamma(6,12),2)
#To prove the resetting hasn't worked:
reset_global_rng(rs)
path3 = rand(Gamma(5,10),2)
println(path1==path3) # false

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 17, 2017, 2:37pm UTC](https://discourse.julialang.org/t/saving-random-number-state-for-statsfuns-distributions-gamma-beta/6504/8 "2017-10-17T14:37:16Z")

</div>

This works for me:

```julia
using Distributions

random_seed = UInt32[0x189bde00, 0xebb1fff3, 0x8ea4d5a7, 0x856e92e5]

srand(random_seed)

path1 = rand(Gamma(5,10),2)
path2 = rand(Gamma(6,12),2)

srand(random_seed)

path3 = rand(Gamma(5,10),2)
println(path1 == path3 ? "equal" : "unequal") # prints equal

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 17, 2017, 2:40pm UTC](https://discourse.julialang.org/t/saving-random-number-state-for-statsfuns-distributions-gamma-beta/6504/9 "2017-10-17T14:40:21Z")

</div>

… but so does your original example. Are you sure you are running this exactly as above? Please post a [gist](https://gist.github.com/) of a _single_ julia source code file, that you run with

```nohighlight
julia path/to/file.jl

```

and version information (for Julia, and at least `Pkg.status("Distributions")`).

---

<div class="post-metadata">

**Author:** ![SteveStruggle](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@SteveStruggle](https://discourse.julialang.org/u/SteveStruggle)\
**Post date:** [October 17, 2017, 3:07pm UTC](https://discourse.julialang.org/t/saving-random-number-state-for-statsfuns-distributions-gamma-beta/6504/10 "2017-10-17T15:07:06Z")

</div>

Thanks for this - I don’t want to manually set a seed value and then later call the same value back (because I want to run some common random sequences after the seed is set). But from your code it is clear that if I can extract the current four element "seed "value of the pertinent random number at the time I want to pause (where my model takes two paths) then my problems are solved - simply reapply it using your code. Obviously though “current state” is better than “seed” as a description of this four vector “seed”. Any thoughts? Many Thanks

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 17, 2017, 3:31pm UTC](https://discourse.julialang.org/t/saving-random-number-state-for-statsfuns-distributions-gamma-beta/6504/11 "2017-10-17T15:31:01Z")

</div>

This was extracted from some RNG state as `Base.Random.GLOBAL_RNG.seed`.

I suspect there is an orthogonal issue here: your example **works** for me, while it apparently did not work for you. The steps I suggested would help investigate why.

---

<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:** [October 17, 2017, 3:34pm UTC](https://discourse.julialang.org/t/saving-random-number-state-for-statsfuns-distributions-gamma-beta/6504/12 "2017-10-17T15:34:40Z")

</div>

It’s still not clear for me why you don’t want to use `srand`: nothing prevents you from using `srand(1234)` at the start of your code snippet, and then to do something like `srand(5678)` before each path?

---

<div class="post-metadata">

**Author:** ![SteveStruggle](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@SteveStruggle](https://discourse.julialang.org/u/SteveStruggle)\
**Post date:** [October 17, 2017, 3:35pm UTC](https://discourse.julialang.org/t/saving-random-number-state-for-statsfuns-distributions-gamma-beta/6504/13 "2017-10-17T15:35:50Z")

</div>

Sorry, are you saying that the very last line on my last posted code returned True (ie my Gamma example)? It returns False on mine (which is my whole problem). If that is the case then it is very strange

---

<div class="post-metadata">

**Author:** ![SteveStruggle](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@SteveStruggle](https://discourse.julialang.org/u/SteveStruggle)\
**Post date:** [October 17, 2017, 3:39pm UTC](https://discourse.julialang.org/t/saving-random-number-state-for-statsfuns-distributions-gamma-beta/6504/14 "2017-10-17T15:39:09Z")

</div>

I don’t want to do this because the example I have given is very simple and my true model wants to do such steps at many different points, often after each other, in unpredictable ways. I do not want to keep going back and having to run past iterations that are not needed if I can keep on storing states.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 17, 2017, 4:05pm UTC](https://discourse.julialang.org/t/saving-random-number-state-for-statsfuns-distributions-gamma-beta/6504/15 "2017-10-17T16:05:02Z")

</div>

Strange indeed, that’s why I asked for further information. Which you are not providing, so I cannot help you any further.

---

<div class="post-metadata">

**Author:** ![SteveStruggle](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@SteveStruggle](https://discourse.julialang.org/u/SteveStruggle)\
**Post date:** [October 17, 2017, 4:51pm UTC](https://discourse.julialang.org/t/saving-random-number-state-for-statsfuns-distributions-gamma-beta/6504/16 "2017-10-17T16:51:09Z")

</div>

I’m using JulioPro 6.0.01 (the Juno setup using Atom - installed about a month ago). On Windows 7. Distributions package is 0.14.2. The code sample I gave was a straight cut and paste from within Juno.

Can anyone else please comment on what results they get from running my code - is the very last expression from my second code posting True or False?
