Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

issue a user warning if random is None #2479

Merged
merged 12 commits into from
Nov 11, 2024
5 changes: 5 additions & 0 deletions mesa/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@
random (Random): the random number generator
"""
if random is None:
warnings.warn(

Check warning on line 123 in mesa/agent.py

View check run for this annotation

Codecov / codecov/patch

mesa/agent.py#L123

Added line #L123 was not covered by tests
"Random number generator not specified, this can make models non-reproducible. Please pass a random number generator explicitly",
UserWarning,
stacklevel=2,
)
random = (
Random()
) # FIXME see issue 1981, how to get the central rng from model
Expand Down
8 changes: 7 additions & 1 deletion mesa/experimental/cell_space/cell_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import itertools
import warnings
from collections.abc import Callable, Iterable, Mapping
from functools import cached_property
from random import Random
Expand Down Expand Up @@ -45,7 +46,12 @@ def __init__(
self._capacity: int = next(iter(self._cells.keys())).capacity

if random is None:
random = Random() # FIXME
warnings.warn(
"Random number generator not specified, this can make models non-reproducible. Please pass a random number generator explicitly",
UserWarning,
stacklevel=2,
)
random = Random()
self.random = random

def __iter__(self): # noqa
Expand Down
6 changes: 6 additions & 0 deletions mesa/experimental/cell_space/discrete_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import warnings
from collections.abc import Callable
from functools import cached_property
from random import Random
Expand Down Expand Up @@ -44,6 +45,11 @@ def __init__(
self.capacity = capacity
self._cells: dict[tuple[int, ...], T] = {}
if random is None:
warnings.warn(
"Random number generator not specified, this can make models non-reproducible. Please pass a random number generator explicitly",
UserWarning,
stacklevel=2,
)
random = Random() # FIXME should default to default rng from model
quaquel marked this conversation as resolved.
Show resolved Hide resolved
self.random = random
self.cell_klass = cell_klass
Expand Down
Loading