Skip to content
Advertisement

How does NumPy seed its random number generators if no seed is provided?

For example, suppose I call numpy.random.uniform(0, 1, 10) without calling any of the seed-related functions. NumPy must be using some default seed, but I couldn’t find it in the documentation. How does NumPy seed its random numbers when no seed is specified?

Advertisement

Answer

For NumPy’s legacy numpy.random.* functions, including numpy.random.uniform, a global RandomState object initialized with no arguments is used. Because a seed isn’t passed to this RandomState, “the MT19937 BitGenerator is initialized by reading data from /dev/urandom (or the Windows analogue) if available or seed from the clock otherwise” (https://numpy.org/doc/stable/reference/random/legacy.html#numpy.random.RandomState).

Likewise, NumPy’s newer BitGenerator classes, such as PCG64, are seeded by default with “fresh, unpredictable entropy … pulled from the OS” (example for default_rng).

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement