numpy.random.Generator.spawn#

method

random.Generator.spawn(n_children)#

Create new independent child generators.

See SeedSequence spawning for additional notes on spawning children.

New in version 1.25.0.

Parameters:
n_childrenint
Returns:
child_generatorslist of Generators
Raises:
TypeError

When the underlying SeedSequence does not implement spawning.

See also

random.BitGenerator.spawn, random.SeedSequence.spawn

Equivalent method on the bit generator and seed sequence.

bit_generator

The bit generator instance used by the generator.

Examples

Starting from a seeded default generator:

>>> # High quality entropy created with: f"0x{secrets.randbits(128):x}"
>>> entropy = 0x3034c61a9ae04ff8cb62ab8ec2c4b501
>>> rng = np.random.default_rng(entropy)

Create two new generators for example for parallel executation:

>>> child_rng1, child_rng2 = rng.spawn(2)

Drawn numbers from each are independent but derived from the initial seeding entropy:

>>> rng.uniform(), child_rng1.uniform(), child_rng2.uniform()
(0.19029263503854454, 0.9475673279178444, 0.4702687338396767)

It is safe to spawn additional children from the original rng or the children:

>>> more_child_rngs = rng.spawn(20)
>>> nested_spawn = child_rng1.spawn(20)