8000 BUG: bit generator spawns different child generators despite having the same random state · Issue #27882 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content
BUG: bit generator spawns different child generators despite having the same random state #27882
Closed
@JakobGruen

Description

@JakobGruen

Describe the issue:

When using the default_rng to spawn child rngs, the random states of the children are different, even if I reset the state of the parent generator.

If the random states are the same, all the generators' functions should produce the same result given the same input. I think this should include spawning children.

Reproduce the code example:

import numpy as np

# create rng and save state
rng = np.random.default_rng()
print(rng.bit_generator.state)
state = rng.bit_generator.state

# spawn a child
child1 = rng.spawn(1)[0]
print(child1.bit_generator.state)
print(child1.uniform(0, 1, 2))

# reset the state and confirm
rng.bit_generator.state = state
print(rng.bit_generator.state == state)

child2 = rng.spawn(1)[0]
print(child2.bit_generator.state)
print(child2.uniform(0, 1, 2))

Error message:

No response

Python and NumPy Versions:

Python 3.12.7

numpy 1.26.4
Also in numpy 2.1.3

Runtime Environment:

No response

Context for the issue:

I am not completely sure if this is a bug or intentional behavior, but it is confusing at least. I could not find any explanation of this behavior in the documentation and it seems counterintuitive.

If the seed is set for the first rng, it still produces different child states, but they are always the same.

I was trying to spawn child and grandchild rngs to be able to reproduce only parts of my simulation without having to rerun everything. But when I tried to recreate it from a child rng, I got different results. If I set the seed in the beginning, I can still recreate the whole simulation and I could create a list of new seeds instead of spawning children, but I would prefer not to do it that way.

Metadata

Metadata

Assignees

No one assigned

    Labels

    33 - QuestionQuestion about NumPy usage or development

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0