create_py_random_state#

create_py_random_state(random_state=None)[source]#

Returns a random.Random instance depending on input.

Parameters:
random_stateint or random number generator or None (default=None)
  • If int, return a random.Random instance set with seed=int.

  • If random.Random instance, return it.

  • If None or the np.random package, return the global random number generator used by np.random.

  • If an np.random.Generator instance, or the np.random package, or the global numpy random number generator, then return it. wrapped in a PythonRandomViaNumpyBits class.

  • If a PythonRandomViaNumpyBits instance, return it.

  • If a PythonRandomInterface instance, return it.

  • If a np.random.RandomState instance and not the global numpy default, return it wrapped in PythonRandomInterface for backward bit-stream matching with legacy code.

Notes

  • A diagram intending to illustrate the relationships behind our support for numpy random numbers is called NetworkX Numpy Random Numbers.

  • More discussion about this support also appears in gh-6869#comment.

  • Wrappers of numpy.random number generators allow them to mimic the Python random number generation algorithms. For example, Python can create arbitrarily large random ints, and the wrappers use Numpy bit-streams with CPython’s random module to choose arbitrarily large random integers too.

  • We provide two wrapper classes: PythonRandomViaNumpyBits is usually what you want and is always used for np.Generator instances. But for users who need to recreate random numbers produced in NetworkX 3.2 or earlier, we maintain the PythonRandomInterface wrapper as well. We use it only used if passed a (non-default) np.RandomState instance pre-initialized from a seed. Otherwise the newer wrapper is used.