Skip to content

Model.reset_rng() creates new RNG instead of restoring initial state #2940

@ShreyasN707

Description

@ShreyasN707

I noticed the RNG behavior in Model, Calling reset_rng() without any arguments doesn’t return the numpy RNG to the state it had when the model was created. Instead, it seems to create a completely new RNG instance seeded from system entropy.

This makes the RNG sequence after a reset different from the sequence right after initialization, even when the model is created with a fixed seed. Since the docstring mentions “if None, reset using the current seed,” this looks unintentional.

The model also stores the initial RNG state in self._rng, but that saved state isn’t used during reset.

  • Expected behavior

If a model is initialized with a deterministic seed, calling:

     model.reset_rng()

should bring the numpy RNG back to its initial state so that the sequence of random values matches what we would get from a fresh model run.

  • To Reproduce
from mesa import Model

model = Model(seed=42)
initial = model.rng.random()

# Move RNG forward
for _ in range(100):
    model.rng.random()

# Try to reset back to initial state
model.reset_rng()

after_reset = model.rng.random()

print(initial, after_reset)
# These two values do not match

This happens consistently: the RNG after reset does not match the original starting state.

@EwoutH I can work on this! Please let me know.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugRelease notes label

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions