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.Randominstance set with seed=int.If
random.Randominstance, return it.If None or the
np.randompackage, return the global random number generator used bynp.random.If an
np.random.Generatorinstance, or thenp.randompackage, or the global numpy random number generator, then return it. wrapped in aPythonRandomViaNumpyBitsclass.If a
PythonRandomViaNumpyBitsinstance, return it.If a
PythonRandomInterfaceinstance, return it.If a
np.random.RandomStateinstance and not the global numpy default, return it wrapped inPythonRandomInterfacefor 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:
PythonRandomViaNumpyBitsis usually what you want and is always used fornp.Generatorinstances. But for users who need to recreate random numbers produced in NetworkX 3.2 or earlier, we maintain thePythonRandomInterfacewrapper as well. We use it only used if passed a (non-default)np.RandomStateinstance pre-initialized from a seed. Otherwise the newer wrapper is used.