Skip to content

Deprecate seed parameter in favor of rng in Model#3147

Merged
quaquel merged 20 commits intomesa:mainfrom
quaquel:deprecate_seed_on_model
Jan 18, 2026
Merged

Deprecate seed parameter in favor of rng in Model#3147
quaquel merged 20 commits intomesa:mainfrom
quaquel:deprecate_seed_on_model

Conversation

@quaquel
Copy link
Copy Markdown
Member

@quaquel quaquel commented Jan 16, 2026

This PR deprecates the seed parameter in Model.__init__() and Model.reset_randomizer() in favor of the SPEC-7 compliant rng parameter. Users will now see a FutureWarning when using seed, encouraging migration to rng.

Migration

This is a simple parameter name change with no behavioral differences:

# Old (now deprecated)
model = MyModel(seed=42)

# New
model = MyModel(rng=42)

The functionality remains identical - both approaches seed the random number generators in the same way. This change is part of aligning Mesa with SPEC-7 (scientific Python community standards for random number generation).

Notes

  • model.random (Python's random.Random) continues to exist and function as before
  • model.rng (NumPy's Generator) continues to exist and function as before
  • Both are seeded identically whether you use seed or rng parameter
  • The deprecation will be removed in Mesa 4.0

Ref: #2352 (original rng implementation)

@github-actions
Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +0.6% [-0.4%, +1.5%] 🔵 +0.3% [+0.1%, +0.4%]
BoltzmannWealth large 🔵 +0.9% [+0.1%, +1.7%] 🔵 +0.5% [-2.6%, +4.8%]
Schelling small 🔵 +1.4% [+1.0%, +1.9%] 🔵 +1.5% [+1.1%, +1.9%]
Schelling large 🔵 +0.4% [-0.5%, +1.5%] 🔵 +0.5% [-2.6%, +3.7%]
WolfSheep small 🔵 -1.5% [-2.0%, -0.9%] 🔵 -1.7% [-2.6%, -0.7%]
WolfSheep large 🔵 +1.1% [-1.1%, +3.2%] 🔵 +3.8% [+0.6%, +7.0%]
BoidFlockers small 🔴 +3.9% [+3.1%, +4.7%] 🔵 +2.7% [+2.3%, +3.2%]
BoidFlockers large 🔵 +0.2% [-0.3%, +0.7%] 🔵 +0.6% [+0.2%, +1.0%]

@quaquel quaquel added the deprecation When a new deprecation is introduced label Jan 16, 2026
@quaquel quaquel requested a review from EwoutH January 16, 2026 17:44
Copy link
Copy Markdown
Member

@EwoutH EwoutH left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this extensive effort, appreciated.

Overal looks good, a (small) note in the migration guide should be added and a few nitpicks.

mesa/model.py Outdated
self._seed = seed # this allows for reproducing stdlib.random
elif rng is None:
warnings.warn(
"the use of seed is deprecated, use rng instead",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add link to migration guide

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is nothing in the migration guide on this. Also, I would argue that the message is all you need. Just use rng instead of seed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While the actual code change is indeed very small, that change is made for a reason and has implications. Therefor I do think an entry is warranted.

Completeness is also important here. The migration guide should contain everything you need to know when updating to a new Mesa version.

It's also a chance to educate our users.

I think something like this would be a good start (feel free to edit of course).

## Mesa 3.5.0
### Deprecation of `seed` in favor of `rng`
The `seed` parameter on `Model.__init__` and the `reset_randomizer` method are deprecated in favor of [[SPEC-7](https://scientific-python.org/specs/spec-0007/)](https://scientific-python.org/specs/spec-0007/) compliant NumPy random number generation.

NumPy's `Generator` class provides higher-quality random number generation compared to Python's built-in `random` module or NumPy's legacy `RandomState`. The new `rng` parameter follows scientific Python community standards and offers better reproducibility controls.

```python
# Old
model = MyModel(seed=42)
model.reset_randomizer(seed=42)

# New
model = MyModel(rng=42)
model.reset_rng(rng=42)
```

Within your model, use `model.rng` (a `numpy.random.Generator`) instead of `model.random` for random number generation. The `rng` parameter accepts integers, sequences of integers, `numpy.random.SeedSequence`, `numpy.random.Generator`, or `numpy.random.BitGenerator`.

* Ref: [PR #2352](https://github.com/mesa/mesa/pull/2352)](https://github.com/mesa/mesa/pull/2352)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But most of what is said in your suggested text is not what this PR does. Model.random still exists. If you used seed and now shift to rng, the model will even behave the same. Really, the only thing that has changed is that we prefer using the rng keyword over the seed keyword, without any change in underlying logic or behavior.

Copy link
Copy Markdown
Member

@EwoutH EwoutH Jan 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, I thought this was a bigger shift.

mesa/model.py Outdated
seed: A new seed for the RNG; if None, reset using the current seed
"""
warnings.warn(
"the use of seed is deprecated, use rng instead",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

link to migration guide

@EwoutH
Copy link
Copy Markdown
Member

EwoutH commented Jan 18, 2026

For my context, is this the first step in a series of deprecations towards SPEC-7 compliance, of is this (for now) the only one?

What's the future of .random()?

I feel somehow we should communicate to our users why we're changing this and how that impacts them.

@quaquel
Copy link
Copy Markdown
Member Author

quaquel commented Jan 18, 2026

For my context, is this the first step in a series of deprecations towards SPEC-7 compliance, of is this (for now) the only one?

This is all that is required to become compliant with spec-7. Spec-7 specifies the use of rng as keyword argument and the use of non-global random number generators. The latter we already have with model.rng and model.random

What's the future of .random()?

To be decided. I prefer fully removing it inside Mesa, but this has profound performance consequences. I also figured out that random.Random, which we use in mesa, is a non-global instance of a seeded stdlib random number generator. So one of my reasons for removing model.random is not valid.

I feel somehow we should communicate to our users why we're changing this and how that impacts them.

Like I said, the impact at the moment is only a change in preferred keyword (to become spec-7 compliant). We could explain this in the release notes.

Copy link
Copy Markdown
Member

@EwoutH EwoutH left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the context.

Minor suggestion on the message, and if we include it in the release notes that should be enough.

@EwoutH EwoutH changed the title Add deprecation warnings for seed on Model.__init__ Deprecate seed parameter in favor of rng in Model Jan 18, 2026
quaquel and others added 2 commits January 18, 2026 11:03
Co-authored-by: Ewout ter Hoeven <[email protected]>
Co-authored-by: Ewout ter Hoeven <[email protected]>
@quaquel quaquel merged commit b6da37e into mesa:main Jan 18, 2026
9 of 12 checks passed
quaquel added a commit to quaquel/mesa that referenced this pull request Jan 18, 2026
@quaquel quaquel deleted the deprecate_seed_on_model branch January 19, 2026 21:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

deprecation When a new deprecation is introduced

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants