mesa icon indicating copy to clipboard operation
mesa copied to clipboard

Migrating from `seed` to `rng` changes model randomness

Open souro26 opened this issue 9 months ago • 6 comments

Describe the bug When initializing a model using seed, Mesa shows the following warning:

The use of the seed keyword argument is deprecated, use rng instead. No functional changes.

However, switching from seed to rng does change behavior. Initializing a model with seed=42 versus rng=np.random.default_rng(42) produces different random streams (both python and numpy), and also results in different Scenario.rng values. This seems to contradict the “no functional changes” part of the warning.

Expected behavior Following the warning’s suggestion to migrate from seed to rng should preserve reproducibility and not change stochastic behavior.

To Reproduce

  • Initialize a model using seed and note the warning emitted from Model.__init__ in mesa/model.py:
  • Initialize the same model using rng=np.random.default_rng(42).
  • Compare the random outputs from both models.
import numpy as np
from mesa.model import Model

class DummyModel(Model):
    def step(self):
        pass

m_seed = DummyModel(seed=42)
m_rng = DummyModel(rng=np.random.default_rng(42))

print("Model.random:", m_seed.random.random(), m_rng.random.random())
print("Model.rng:", m_seed.rng.random(), m_rng.rng.random())
print("Scenario.rng:", m_seed.scenario.rng, m_rng.scenario.rng)
  • Observe that the random values differ, and that Scenario.rng is different between the two initialization paths (defined in mesa/model.py and mesa/experimental/scenarios/scenario.py).

Additional context This was observed while reviewing the seed to rng migration path and verifying the “no functional changes” guarantee during model initialization.

souro26 avatar Feb 07 '26 11:02 souro26

I've reproduced this bug in the latest development branch and identified the root cause in model.py. The issue is that np.random.default_rng() creates a new Generator when passed an existing one, breaking reproducibility.

I have a fix ready with comprehensive tests. I'll submit a PR shortly.

Proposed fix: Add isinstance(rng, np.random.Generator) check in both Model.init() and Model.reset_rng() to preserve Generator identity.

Jayantparashar10 avatar Feb 07 '26 13:02 Jayantparashar10

This is at least in part a semantic discussion. For me numerical reproducibility is not the same as function reproducibility.

quaquel avatar Feb 07 '26 14:02 quaquel

Thanks for looking into this @Jayantparashar10 .

Based on @quaquel’s comment on #3254 , it seems the divergence observed isn’t explained solely by generator identity, since np.random.default_rng() should already preserve an incoming Generator. That suggests the behavioral difference is coming from elsewhere in the initialization path (e.g. how stdlib random and Scenario.rng are derived when using seed vs rng). I’ll dig a bit deeper into where the streams first diverge and update the issue along with a PR once I have a clearer picture.

souro26 avatar Feb 07 '26 14:02 souro26

Hi @souro26, thanks for the detailed reproduction,

I agree with @quaquel about the reproducibility argument and I also don't see it as a bug. You are correct that seed=42 and rng=np.random.default_rng(42) produce different streams. This is expected behavior due to how the Model synchronizes Python's random with Numpy's rng. When you pass a Generator object (like default_rng(42)), Python's random library cannot read its internal state directly. To synchronize them, the Model must draw a random integer from the Generator to seed random(see L146).

Consequence: This drawing operation advances the Numpy Generator by one step. Therefore, the subsequent numbers it produces will be shifted compared to the fresh state of seed=42.

If you want exact numerical reproducibility when migrating from seed, you should pass the integer directly to rng, rather than a Generator object.

https://github.com/mesa/mesa/blob/780e43365db2a372ec6c9b1f708aac64abc673a9/mesa/model.py#L140-L146

codebreaker32 avatar Feb 07 '26 18:02 codebreaker32

I agree with @codebreaker32 take. I just checked it myself, but if I pass 42 to rng, I do get the same as when I pass 42 to seed.

I am closing this as not a bug.

quaquel avatar Feb 07 '26 18:02 quaquel

Thanks for the clarification @codebreaker32. After going through it i agree it's not a bug as well. Appreciate the detailed explanation.

souro26 avatar Feb 07 '26 19:02 souro26