mesa icon indicating copy to clipboard operation
mesa copied to clipboard

Fix random number generator warnings

Open Mani212005 opened this issue 2 weeks ago • 1 comments

Description This PR addresses the random number generator warnings that were causing ~45+ UserWarnings across the test suite. The warnings indicated that models could be non-reproducible when RNG is not explicitly specified.

Changes Made:

  1. Added shared rng fixture (tests/conftest.py)
    • Created a pytest fixture that provides a deterministic [random.Random(42)]( instance
    • Can be used across all test files for reproducible randomness
  2. Fixed Conway's Game of Life example ([model.py]
    • Added [random=self.random]) parameter to initialization
    • Ensures the grid uses the model's seeded RNG for reproducibility
  3. Added tests using the rng fixture (tests/test_discrete_space.py)
    • test_orthogonal_moore_grid_with_rng: Tests Moore grid with provided RNG
    • test_orthogonal_von_neumann_grid_with_rng: Tests Von Neumann grid with provided RNG
    • test_cell_agent_with_rng: Tests CellAgent with provided RNG

Testing:

pytest tests/test_discrete_space.py -v

Result: 30 passed

pytest tests/ -v 2>&1 | grep -i "random number generator not specified"

Result: No warnings found

Impact ✅ Eliminates all "Random number generator not specified" warnings ✅ Improves model reproducibility ✅ No breaking changes - backwards compatible ✅ Provides pattern for future test development with the rng fixture

Closes # 2904 part 2.1 . Thank you.

Mani212005 avatar Dec 05 '25 16:12 Mani212005

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +0.1% [-0.2%, +0.5%] 🔵 +1.0% [+0.9%, +1.1%]
BoltzmannWealth large 🔵 +3.0% [-13.4%, +23.9%] 🔵 +2.3% [-0.6%, +5.1%]
Schelling small 🔵 +0.1% [-0.1%, +0.3%] 🔵 -0.8% [-0.9%, -0.6%]
Schelling large 🔵 +6.5% [-10.5%, +25.1%] 🔵 +10.8% [-9.9%, +41.0%]
WolfSheep small 🔵 -0.7% [-0.9%, -0.5%] 🔵 -0.2% [-0.3%, -0.1%]
WolfSheep large 🔵 +12.6% [-27.8%, +66.9%] 🔵 +21.2% [-14.0%, +62.9%]
BoidFlockers small 🔵 +1.2% [+0.8%, +1.6%] 🔵 +0.9% [+0.8%, +1.0%]
BoidFlockers large 🔵 +1.4% [+1.0%, +1.8%] 🔵 +0.5% [+0.3%, +0.7%]

github-actions[bot] avatar Dec 05 '25 17:12 github-actions[bot]