Skip to content

Commit

Permalink
Bugfix for non deterministic rng behavior (#2598)
Browse files Browse the repository at this point in the history
* bugfix

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
quaquel and pre-commit-ci[bot] authored Jan 6, 2025
1 parent 9de3dab commit 696923f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
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

0 comments on commit 696923f

Please sign in to comment.