Skip to content

Commit

Permalink
issue a warning if random is none
Browse files Browse the repository at this point in the history
  • Loading branch information
quaquel committed Nov 10, 2024
1 parent c078690 commit 076f943
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
2 changes: 2 additions & 0 deletions mesa/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ def __init__(self, agents: Iterable[Agent], random: Random | None = None):
random (Random): the random number generator
"""
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 see issue 1981, how to get the central rng from model
Expand Down
5 changes: 4 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,9 @@ 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
3 changes: 3 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,8 @@ 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
self.random = random
self.cell_klass = cell_klass
Expand Down

0 comments on commit 076f943

Please sign in to comment.