scipy icon indicating copy to clipboard operation
scipy copied to clipboard

BUG: copied/pickled dists lose connection to RandomState

Open JelleAalbers opened this issue 2 years ago • 0 comments

This is a possible fix for issue #16998 -- assuming I correctly interpreted it as a bug / undesirable behaviour.

After this, distributions initialized without a custom random state will couple to numpy's global random state, even after an unpickle or deepcopy. Nothing changes for distributions that have custom random states/seeds set.

Implementation details:

  • The _random_state attribute of distributions can now be None, signifying compliance with the global random state. The random_state property converts this None into the current numpy RandomState when needed.
  • Replaced self._random_state with self.random_state in a few methods that probably predate #4340
  • Added tests for regular and frozen distributions. We still test that explicitly-set random states are copied (as before).

Nothing changes for existing pickles of distributions either. We could not change that even if we wanted to: those pickles contain RandomStates in their attributes, without a way to tell whether those were intentionally specified or not.

(The linux/mac meson build failures mirror those on the current master)

JelleAalbers avatar Sep 10 '22 23:09 JelleAalbers