Skip to content

Reproducibilty Trap in model.py #3190

@codebreaker32

Description

@codebreaker32

This code block is really confusing me

mesa/mesa/model.py

Lines 122 to 127 in b6e96d1

try:
self.random = random.Random(rng)
except TypeError:
seed = int(self.rng.integers(np.iinfo(np.int32).max))
self.random = random.Random(seed)
self._seed = seed # this allows for reproducing stdlib.random

It uses a try-except block to handle compatibility issues but the logic fails to capture the seed value in the successful cases.

  • If rng is None: random.Random(None) succeeds, skipping the except block. seed remains None.
  • If rng is an int: random.Random(int) succeeds, skipping the except block. seed remains None.

As a result, self._seed is set to None at the end of the block (self._seed = seed). This breaks reproducibility and causes model.scenario.rng to be None

Please correct me if I am wrong!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions