diff --git a/mesa/model.py b/mesa/model.py index 15374f70796..496d35279b0 100644 --- a/mesa/model.py +++ b/mesa/model.py @@ -7,6 +7,7 @@ # Remove this __future__ import once the oldest supported Python is 3.10 from __future__ import annotations +import datetime import random # mypy @@ -21,7 +22,16 @@ class Model: def __new__(cls, *args: Any, **kwargs: Any) -> Any: """Create a new model object and instantiate its RNG automatically.""" obj = object.__new__(cls) - obj._seed = kwargs.get("seed", None) + obj._seed = kwargs.get("seed") + if obj._seed is None: + # See https://docs.python.org/3/library/random.html#random.seed + # which says "If a is omitted or None, the current system time is used." + # We explicitly specify the seed here so that we know its value. + # datetime.now().timestamp() is more cross-platform than using + # time.time(), and guarantees that the precision is to the + # microsecond. + current_timestamp = datetime.datetime.now(datetime.timezone.utc).timestamp() + obj._seed = current_timestamp obj.random = random.Random(obj._seed) return obj