Skip to content

Bugfix for non deterministic rng behavior #2598

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 2 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mesa/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def __init__(
self._seed = seed # this allows for reproducing stdlib.random

try:
self.rng: np.random.Generator = np.random.default_rng(rng)
self.rng: np.random.Generator = np.random.default_rng(seed)
except TypeError:
rng = self.random.randint(0, sys.maxsize)
self.rng: np.random.Generator = np.random.default_rng(rng)
Expand Down
12 changes: 12 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Tests for model.py."""

import numpy as np

from mesa.agent import Agent, AgentSet
from mesa.model import Model

Expand Down Expand Up @@ -37,6 +39,16 @@ def test_seed(seed=23):
assert model2._seed == seed + 1
assert model._seed == seed

assert Model(seed=42).random.random() == Model(seed=42).random.random()
assert np.all(
Model(seed=42).rng.random(
10,
)
== Model(seed=42).rng.random(
10,
)
)


def test_reset_randomizer(newseed=42):
"""Test resetting the random seed on the model."""
Expand Down
Loading