# How to Spawn Child RNGs with Random?

**URL:** <https://discourse.julialang.org/t/how-to-spawn-child-rngs-with-random/102122>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 26, 2023, 2:40pm UTC](https://discourse.julialang.org/t/how-to-spawn-child-rngs-with-random/102122 "2023-07-26T14:40:59Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alegresor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alegresor/32/51833_2.png) [@alegresor](https://discourse.julialang.org/u/alegresor)\
**Post date:** [July 26, 2023, 2:40pm UTC](https://discourse.julialang.org/t/how-to-spawn-child-rngs-with-random/102122/1 "2023-07-26T14:40:59Z")

</div>

In Python I can spawn independent child RNGs with [numpy.random.Generator.spawn](https://numpy.org/doc/stable/reference/random/generated/numpy.random.Generator.spawn.html#numpy.random.Generator.spawn). How can I do this in Julia?

---

<div class="post-metadata">

**Author:** ![Alexander\_Knudson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexander_knudson/32/215656_2.png) [@Alexander\_Knudson](https://discourse.julialang.org/u/Alexander_Knudson)\
**Post date:** [July 26, 2023, 3:03pm UTC](https://discourse.julialang.org/t/how-to-spawn-child-rngs-with-random/102122/2 "2023-07-26T15:03:33Z")

</div>

Hi alegresor, welcome to the Julia discourse!

I’m not sure that Julia has an explicit splittable RNG, but the TaskLocalRNG automatically spawns and seeds independent RNGs from the parent RNG in a per-task way.

The Xoshiro RNG (which you can read about [here](http://prng.di.unimi.it)) will also implicitly spawn independent RNGs when doing bulk random number generation (see [here](https://docs.julialang.org/en/v1/stdlib/Random/#Random.Xoshiro)).

If you still need to be able to split the RNG, then I would recommend writing the “jump” function described in the first link I sent, and then writing a “split” method that copies the parent Xoshiro, then jumps the parent state using the jump method.

I’ll see if I can offer a concrete example later on if you still need a solution 🙂

---

<div class="post-metadata">

**Author:** ![Alexander\_Knudson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexander_knudson/32/215656_2.png) [@Alexander\_Knudson](https://discourse.julialang.org/u/Alexander_Knudson)\
**Post date:** [July 26, 2023, 4:22pm UTC](https://discourse.julialang.org/t/how-to-spawn-child-rngs-with-random/102122/3 "2023-07-26T16:22:00Z")

</div>

Here is a concrete example for my earlier reply

```julia
using Random

function jump!(rng::Xoshiro)
    JUMP = [
        0x180ec6d33cfd0aba, 
        0xd5a61266f0c9392c, 
        0xa9582618e03fc9aa, 
        0x39abdc4529b1661c
    ]

    s0 = zero(UInt64)
    s1 = zero(UInt64)
    s2 = zero(UInt64)
    s3 = zero(UInt64)

    for j in JUMP
        for b in 0:63
            if (j & one(UInt64) << b) > 0
                s0 ⊻= rng.s0
                s1 ⊻= rng.s1
                s2 ⊻= rng.s2
                s3 ⊻= rng.s3
            end
            rand(rng)
        end
    end

    rng.s0 = s0
    rng.s1 = s1
    rng.s2 = s2
    rng.s3 = s3

    return rng
end

function spawn!(rng::Xoshiro)
    child_rng = copy(rng)
    jump!(rng)
    return child_rng
end

```

The `jump!` method is equivalent to 2^128 calls to the `rand()` function. So an independent RNG can be spawned from the parent Xoshiro by first cloning the parent RNG, then jumping its state to 2^128 places later in the sequence. Using this process, you can generate 2^128 “non-overlapping subsequences for parallel computations.”

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [July 26, 2023, 4:41pm UTC](https://discourse.julialang.org/t/how-to-spawn-child-rngs-with-random/102122/4 "2023-07-26T16:41:14Z")

</div>

Some of the code and pointers in this previous discussion may be of interest: [Does Julia have any utility to "split" RNGs for later use in parallel? - #4 by marius311](https://discourse.julialang.org/t/does-julia-have-any-utility-to-split-rngs-for-later-use-in-parallel/82775/4)

---

<div class="post-metadata">

**Author:** ![alegresor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alegresor/32/51833_2.png) [@alegresor](https://discourse.julialang.org/u/alegresor)\
**Post date:** [July 26, 2023, 5:42pm UTC](https://discourse.julialang.org/t/how-to-spawn-child-rngs-with-random/102122/5 "2023-07-26T17:42:27Z")

</div>

Excellent, thanks!

---

<div class="post-metadata">

**Author:** ![alegresor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alegresor/32/51833_2.png) [@alegresor](https://discourse.julialang.org/u/alegresor)\
**Post date:** [July 26, 2023, 5:59pm UTC](https://discourse.julialang.org/t/how-to-spawn-child-rngs-with-random/102122/6 "2023-07-26T17:59:13Z")

</div>

> [@Alexander\_Knudson](#):
>
> `rng.s2 = s3`

I think this should be `rng.s3 = s3`
