mesa icon indicating copy to clipboard operation
mesa copied to clipboard

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

Open ShreyasN707 opened this issue 2 weeks ago • 1 comments

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.

ShreyasN707 avatar Dec 10 '25 06:12 ShreyasN707

@quaquel is this because of initializing the model with seed instead of rng?

EwoutH avatar Dec 10 '25 07:12 EwoutH

This clearly is a bug. I'll submit a PR fixing it as soon as possible.

quaquel avatar Dec 11 '25 07:12 quaquel

This clearly is a bug. I'll submit a PR fixing it as soon as possible.

I would like to raise the PR please, I am a already working on it!

ShreyasN707 avatar Dec 11 '25 07:12 ShreyasN707

See PR #2946 for the fix.

quaquel avatar Dec 11 '25 08:12 quaquel

@ShreyasN707, sorry, I didn't see your message and quickly fixed it myself.

quaquel avatar Dec 11 '25 08:12 quaquel

@ShreyasN707, sorry, I didn't see your message and quickly fixed it myself.

I really wanted to fix it. Anyways it's fine! No worries.

ShreyasN707 avatar Dec 11 '25 08:12 ShreyasN707