What Does Numpy.Random.Seed(0) Do

What does numpy.random.seed(0) do?

np.random.seed(0) makes the random numbers predictable

>>> numpy.random.seed(0) ; numpy.random.rand(4)
array([ 0.55, 0.72, 0.6 , 0.54])
>>> numpy.random.seed(0) ; numpy.random.rand(4)
array([ 0.55, 0.72, 0.6 , 0.54])

With the seed reset (every time), the same set of numbers will appear every time.

If the random seed is not reset, different numbers appear with every invocation:

>>> numpy.random.rand(4)
array([ 0.42, 0.65, 0.44, 0.89])
>>> numpy.random.rand(4)
array([ 0.96, 0.38, 0.79, 0.53])

(pseudo-)random numbers work by starting with a number (the seed), multiplying it by a large number, adding an offset, then taking modulo of that sum. The resulting number is then used as the seed to generate the next "random" number. When you set the seed (every time), it does the same thing every time, giving you the same numbers.

If you want seemingly random numbers, do not set the seed. If you have code that uses random numbers that you want to debug, however, it can be very helpful to set the seed before each run so that the code does the same thing every time you run it.

To get the most random numbers for each run, call numpy.random.seed(). This will cause numpy to set the seed to a random number obtained from /dev/urandom or its Windows analog or, if neither of those is available, it will use the clock.

For more information on using seeds to generate pseudo-random numbers, see wikipedia.

Why `seed()` method isn't part of `randint()` function?

A seed is meant to determine a sequence of RNG results. Like this:

In [1]: import numpy

In [2]: numpy.random.seed(4)

In [3]: numpy.random.randint(0, 10, 10)
Out[3]: array([7, 5, 1, 8, 7, 8, 2, 9, 7, 7])

In [4]: numpy.random.randint(0, 10, 10)
Out[4]: array([7, 9, 8, 4, 2, 6, 4, 3, 0, 7])

In [5]: numpy.random.randint(0, 10, 10)
Out[5]: array([5, 5, 9, 6, 6, 8, 2, 5, 8, 1])

In [6]: numpy.random.randint(0, 10, 10)
Out[6]: array([2, 7, 0, 8, 3, 1, 0, 3, 2, 3])

In [7]: numpy.random.seed(4)

In [8]: numpy.random.randint(0, 10, 10)
Out[8]: array([7, 5, 1, 8, 7, 8, 2, 9, 7, 7])

In [9]: numpy.random.randint(0, 10, 10)
Out[9]: array([7, 9, 8, 4, 2, 6, 4, 3, 0, 7])

In [10]: numpy.random.randint(0, 10, 10)
Out[10]: array([5, 5, 9, 6, 6, 8, 2, 5, 8, 1])

In [11]: numpy.random.randint(0, 10, 10)
Out[11]: array([2, 7, 0, 8, 3, 1, 0, 3, 2, 3])

See how after the second seed call (on line In [7]), the sequence resets?

When you set a seed, the RNG output still has the same statistical properties, but you can run the program again with the same seed and get the same results. This is useful for things like debugging, or reproducible simulations.


If seed were part of randint, that would reset the sequence every time. It would look like this:

In [12]: numpy.random.seed(4)

In [13]: numpy.random.randint(0, 10, 10)
Out[13]: array([7, 5, 1, 8, 7, 8, 2, 9, 7, 7])

In [14]: numpy.random.seed(4)

In [15]: numpy.random.randint(0, 10, 10)
Out[15]: array([7, 5, 1, 8, 7, 8, 2, 9, 7, 7])

In [16]: numpy.random.seed(4)

In [17]: numpy.random.randint(0, 10, 10)
Out[17]: array([7, 5, 1, 8, 7, 8, 2, 9, 7, 7])

In [18]: numpy.random.seed(4)

In [19]: numpy.random.randint(0, 10, 10)
Out[19]: array([7, 5, 1, 8, 7, 8, 2, 9, 7, 7])

Same results on every single call. Producing the same results on every call is not how we want RNG output to behave.

random.seed(): What does it do?

Pseudo-random number generators work by performing some operation on a value. Generally this value is the previous number generated by the generator. However, the first time you use the generator, there is no previous value.

Seeding a pseudo-random number generator gives it its first "previous" value. Each seed value will correspond to a sequence of generated values for a given random number generator. That is, if you provide the same seed twice, you get the same sequence of numbers twice.

Generally, you want to seed your random number generator with some value that will change each execution of the program. For instance, the current time is a frequently-used seed. The reason why this doesn't happen automatically is so that if you want, you can provide a specific seed to get a known sequence of numbers.

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

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).

How Does Numpy Random Seed Changes?

You're only looking at part of the state. The big array of 624 integers isn't the whole story.

The Mersenne Twister only updates its giant internal state array once every 624 calls. The rest of the time, it just reads an element of that array, feeds it through a "tempering" pass, and outputs the tempered result. It only updates the array on the first call, or once it's read every element.

To keep track of the last element it read, the Mersenne Twister has an additional position variable that you didn't account for. It's at index 2 in the get_state() tuple. You'll see it increment in steps of 2 in your loop, because np.random.rand() has to fetch 2 32-bit integers to build a single double-precision floating point output.

(NumPy also maintains some additional state that's not really part of the Mersenne Twister state, to generate normally distributed values more efficiently. You'll find this in indexes 3 and 4 of the get_state() tuple.)

Does numpy.random.seed make results fixed on different computers?

It all depends upon type of algorithm implemented internally by numpy random function. In case of numpy, which is operated by pseudo-random number generators (PRNGs) algorithm. What this means is that if you provide the same seed( as of starting input ), you will get the same output. And if you change the seed, you will get a different output. So this kind of algorithm is no system dependent.

But for a true random number generator (TRNG) these often rely on some kind of specialized hardware that does some physical measurement of something unpredictable in the environment such as light or temperature electrical noise radioactive material. So if an module implements t
his kind of algorithm then it will be system dependent.



Related Topics



Leave a reply



Submit