Model.reset_rng() creates new RNG instead of restoring initial state
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.
@quaquel is this because of initializing the model with seed instead of rng?
This clearly is a bug. I'll submit a PR fixing it as soon as possible.
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!
See PR #2946 for the fix.
@ShreyasN707, sorry, I didn't see your message and quickly fixed it myself.
@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.