Skip to content

Deprecating stdlib random #2884

@quaquel

Description

@quaquel

Mesa currently supports both the sdlib random library (via model.random) and numpy's random number generators (via model.rng). While disucssing a fix for a bug in the batchrunner, I mentioned in passing that it might be good to shift all internals over to numpy's random number generation.

Why? Python's stdlib random uses Mersenne Twister and only exists at the global level. Both are a problem. Mersenne Twister is a proven but by now quite dated algorithm for the generation of pseudo-random numbers with various known issues. Most importantly, adjacent seed values (eg., 42, 43, 44) produce very similar random numbers, and using low integers (e.g., 42, 101) as a seed results in poor sequences of random numbers.

Stdlib random only has global state, so whether you use model.random, or just random does not matter. Moreover, if you have two models running side by side using, e.g., threading, you get race conditions, and you cannot guarantee reproducibility.

Both problems are solved in numpy's random number generators. You can have multiple independent instances of a generator (so, no global state, only instance-level state), and the algorithms available produce higher-quality sequences of random numbers and are much less sensitive to the values with which the algorithm is seeded.

Therefore, I propose deprecating model.random and favoring model.rng. Moreover, I would like to do this in a spec 7 compliant way. However, before we can do this, all internals will have to be moved over. A quick first list (to be updated) of classes that will require changes is

  • AgentSet
  • All discrete spaces
  • CellCollection
  • ContinuousSpace

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions