Skip to content

Commit 44276fb

Browse files
Pass through model.rgn in agent analogous to model.random (#2400)
* pass through model.rgn in agent analogous to model.random * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add test docstring for ruff --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 3c0cd62 commit 44276fb

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

mesa/agent.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
# mypy
2222
from typing import TYPE_CHECKING, Any, Literal, overload
2323

24+
import numpy as np
25+
2426
if TYPE_CHECKING:
2527
# We ensure that these are not imported during runtime to prevent cyclic
2628
# dependency.
@@ -85,9 +87,14 @@ def advance(self) -> None: # noqa: D102
8587

8688
@property
8789
def random(self) -> Random:
88-
"""Return a seeded rng."""
90+
"""Return a seeded stdlib rng."""
8991
return self.model.random
9092

93+
@property
94+
def rng(self) -> np.random.Generator:
95+
"""Return a seeded np.random rng."""
96+
return self.model.rng
97+
9198

9299
class AgentSet(MutableSet, Sequence):
93100
"""A collection class that represents an ordered set of agents within an agent-based model (ABM).

tests/test_agent.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,14 @@ def test_agent_membership():
162162
assert AgentTest(model) not in agentset
163163

164164

165+
def test_agent_rng():
166+
"""Test whether agent.random and agent.rng are equal to model.random and model.rng."""
167+
model = Model(seed=42)
168+
agent = Agent(model)
169+
assert agent.random is model.random
170+
assert agent.rng is model.rng
171+
172+
165173
def test_agent_add_remove_discard():
166174
"""Test adding, removing and discarding agents from AgentSet."""
167175
model = Model()

0 commit comments

Comments
 (0)