# Recreate a random number series when not using Random.seed!(my\_seed)

**URL:** https://discourse.julialang.org/t/recreate-a-random-number-series-when-not-using-random-seed-my-seed/88038
**Category:** General Usage
**Tags:** random
**Created:** [September 30, 2022, 1:08pm UTC](https://discourse.julialang.org/t/recreate-a-random-number-series-when-not-using-random-seed-my-seed/88038 "2022-09-30T13:08:11Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![ArchieCall](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/archiecall/32/206_2.png) [@ArchieCall](https://discourse.julialang.org/u/ArchieCall)
#### Post date: [September 30, 2022, 1:08pm UTC](https://discourse.julialang.org/t/recreate-a-random-number-series-when-not-using-random-seed-my-seed/88038/1 "2022-09-30T13:08:12Z")

</div>

Is there a way to get back a random number series when Random.seed!(my\_seed) was not used to create the series.

One thought I had was to randomly generate my\_seed and then use that to gen the series in the Random.seed! command. This is kind of a self defeating argument.

Maybe there is some elegant function for this?

---

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [September 30, 2022, 2:37pm UTC](https://discourse.julialang.org/t/recreate-a-random-number-series-when-not-using-random-seed-my-seed/88038/2 "2022-09-30T14:37:26Z")

</div>

Are you asking how to compute the seed from the random sequence?

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [September 30, 2022, 4:32pm UTC](https://discourse.julialang.org/t/recreate-a-random-number-series-when-not-using-random-seed-my-seed/88038/3 "2022-09-30T16:32:42Z")

</div>

Your method will be hard. RNGs are designed to generate uncorrelated sequences, so you will have to try all possible integers (or exploit some advanced tricks used by the cryptographers I guess…)

---

<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: [September 30, 2022, 4:41pm UTC](https://discourse.julialang.org/t/recreate-a-random-number-series-when-not-using-random-seed-my-seed/88038/4 "2022-09-30T16:41:21Z")

</div>

There are cryptographic RNGs where it wouldn’t work, but don’t rely on this NOT being possible with Julia’s default RNG. The seed ~~is UInt64~~ (max, though can also take a Vector of) so should only take a few hours to do. See below in my later post, if 4 are used as I think it is, then forget it.

```julia
help?> Random.seed!(10) # works but not for seed! so it seems not exported (from Random), but shouldn't it be, also for helping with help search?

```

---

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [September 30, 2022, 5:47pm UTC](https://discourse.julialang.org/t/recreate-a-random-number-series-when-not-using-random-seed-my-seed/88038/5 "2022-09-30T17:47:29Z")

</div>

Could look into the default seeding procedure as well, perhaps it’s from a clock, and you can restrict to a particular range based on when the original sequence was made?

---

<div class="post-metadata">

### Author: ![ArchieCall](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/archiecall/32/206_2.png) [@ArchieCall](https://discourse.julialang.org/u/ArchieCall)
#### Post date: [September 30, 2022, 5:51pm UTC](https://discourse.julialang.org/t/recreate-a-random-number-series-when-not-using-random-seed-my-seed/88038/6 "2022-09-30T17:51:03Z")

</div>

Yes. Given that I used a random number series without seeding first, then what is the my\_seed variable that would have created that series if I had used Random.seed!(my\_seed) first?

---

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [September 30, 2022, 5:52pm UTC](https://discourse.julialang.org/t/recreate-a-random-number-series-when-not-using-random-seed-my-seed/88038/7 "2022-09-30T17:52:12Z")

</div>

Be aware this will also depend on the version of Julia as the default random number generator has changed through time.

---

<div class="post-metadata">

### Author: ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)
#### Post date: [September 30, 2022, 6:04pm UTC](https://discourse.julialang.org/t/recreate-a-random-number-series-when-not-using-random-seed-my-seed/88038/8 "2022-09-30T18:04:24Z")

</div>

Also, the rng state consists of 4 `UInt64`, while a seed is only one.  
Most states will not have any seeds that map to them.

---

<div class="post-metadata">

### Author: ![ArchieCall](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/archiecall/32/206_2.png) [@ArchieCall](https://discourse.julialang.org/u/ArchieCall)
#### Post date: [September 30, 2022, 6:19pm UTC](https://discourse.julialang.org/t/recreate-a-random-number-series-when-not-using-random-seed-my-seed/88038/9 "2022-09-30T18:19:41Z")

</div>

Thanks to all. I solved this by first creating my\_seed with rand(1:10000) and then using that in Random.seed!(my\_seed). I created a file which initially holds my\_seed. This can then be used to recreate the sequence for debugging purposes in case my program bombs due to a logic bug. My program creates thousands of different sequence for simulation purposes.

---

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [September 30, 2022, 7:10pm UTC](https://discourse.julialang.org/t/recreate-a-random-number-series-when-not-using-random-seed-my-seed/88038/10 "2022-09-30T19:10:09Z")

</div>

> [@ArchieCall](#):
>
> first creating my\_seed with rand(1:10000) and then using that in Random.seed!(my\_seed)

I see, you want to be able to recreate each simulation, so you first create a set of seeds, one for each simulation?

There are two ways to go about this. The first is to not worry about potential correlations and just use the seeds in order, for ease of use… Seed the nth simulation with the number n.

The other is to try to spread the seeds out in a way that avoids correlations between simulations in which case I’d recommend reading 64 bit ints from /dev/urandom on Linux, or some similar process on Windows, and storing those… I don’t think random(1:10000) is a good choice.

---

<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: [September 30, 2022, 7:13pm UTC](https://discourse.julialang.org/t/recreate-a-random-number-series-when-not-using-random-seed-my-seed/88038/11 "2022-09-30T19:13:31Z")

</div>

As of Julia 1.7 MersenneTwister is no longer the default RNG, and the seed is 4 UInt64, and if I’ve reading correctly, i.e. all set, then you will never recover the seed.

> <https://github.com/JuliaLang/julia/blob/99225ab1d11925b8e904b0b12f1ee8b0f6946660/stdlib/Random/src/RNGs.jl#L356>

For the older (used in Julia 1.6 and before):

> Create a `MersenneTwister` RNG object. Different RNG objects can have  
> their own seeds, which may be useful for generating different streams  
> of random numbers.
> 
> The `seed` may be a non-negative integer or a vector of  
> `UInt32` integers. If no seed is provided, a randomly generated one  
> is created (using entropy from the system).

---

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [September 30, 2022, 7:16pm UTC](https://discourse.julialang.org/t/recreate-a-random-number-series-when-not-using-random-seed-my-seed/88038/12 "2022-09-30T19:16:09Z")

</div>

> [@dlakelan](#):
>
> reading 64 bit ints from /dev/urandom on Linux, or some similar process on Windows, and storing those… I don’t think random(1:10000) is a good choice.

It looks like this can be done portably by using `rand(RandomDevice(),10000)`

---

<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 1, 2022, 9:45am UTC](https://discourse.julialang.org/t/recreate-a-random-number-series-when-not-using-random-seed-my-seed/88038/13 "2022-10-01T09:45:11Z")

</div>

> [@dlakelan](#):
>
> The first is to not worry about potential correlations and just use the seeds in order, for ease of use… Seed the nth simulation with the number n.

Do note that for `Xoshiro` (and `TaskLocalRGN`, the default RNG), it should be fine to use sequential seeds, and they go through SHA2\_256, and the result of that cryptographic hash is used as the state.
