Skip to content

Commit

Permalink
model: Ensure the seed is initialized with current timestamp when it …
Browse files Browse the repository at this point in the history
…is None
  • Loading branch information
rht committed Sep 20, 2023
1 parent 3dbabfe commit 05caedc
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion mesa/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from __future__ import annotations

import random
from datetime import datetime

# mypy
from typing import Any
Expand All @@ -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.now().timestamp()

Check failure on line 33 in mesa/model.py

View workflow job for this annotation

GitHub Actions / lint-ruff

Ruff (DTZ005)

mesa/model.py:33:33: DTZ005 The use of `datetime.datetime.now()` without `tz` argument is not allowed
obj._seed = current_timestamp
obj.random = random.Random(obj._seed)
return obj

Expand Down

0 comments on commit 05caedc

Please sign in to comment.