add tag
I'm trying to set the seed for a few lines of code in a jupyter notebook. 

However, when I tried to run numpy.random.seed(0) in the initial cell, in the later cells the random generator was not 'seeded'. I had to write a set seed in all the cells where I was running the random draws... Why is that? Is there a way to avoid having to write the same thing in every cell where I have a random draw? Here's an example:

import numpy as np

 I got 
 array([ 0.25739993, -0.90848143, -0.37850311, -0.5349156 ,  0.85807335,
        -0.41300998,  0.49818858,  2.01019925,  1.26286154, -0.43921486,
        -0.34643789,  0.45531966, -1.66866271, -0.8620855 ,  0.49291085,
        -0.1243134 ,  1.93513629, -0.61844265, -1.04683899, -0.88961759])
Now, when I ran `np.random.normal(0,1,20)` again in a new cell, I got

array([ 0.01404054, -0.16082969,  2.23035965, -0.39911572,  0.05444456,
        0.88418182, -0.10798056,  0.55560698,  0.39490664,  0.83720502,
       -1.40787817,  0.80784941, -0.13828364,  0.18717859, -0.38665814,
        1.65904873, -2.04706913,  1.39931699, -0.67900712,  1.52898513])
So, despite having seeded the random generator, the random draws were not equal... Is there a way to solve this?

Also, I checked the [numpy documentation](
)  and it seems that `numpy.random.seed()` is not the best practice. I don't understand the example given there... How would I go about, according to the best practices shown in the examples, in order to do something similar to `numpy.random.seed(constant)`?

Top Answer
A pseudo-random number generator can be modelled as an infinite stream of bits. Every time you generate a number using the pseudo-random number generator, it shifts the stream along, returning and discarding the bits at the beginning. Seeding the PRNG discards the current stream and selects the one specified by the seed.

Each time you generate a number from the PRNG, _that many bits are removed from it_. This means that the PRNG states changes each time you generate a number from it. If you don't reset the PRNG's state by re-seeding it, it'll be a while (centuries) before its output repeats.

There is a workaround: create a new PRNG each time.

    import numpy as np
    np.random.RandomSeed(43).normal(0, 1, 20)

You could create a convenience function:

    def pseudorandom(seed):
        return np.random.RandomSeed(seed)
    pseudorandom(43).normal(0, 1, 20)

Note that if you are repeatedly re-seeding your PRNG, it will **not give you secure numbers** (not that this PRNG is secure _anyway_). They might _look_ random, but they're very predictable; all you need to guess is the seed. To be on the safe side, treat them like they are _entirely_ predictable.
Answer #2
I would like to tie up the answer from WizzWizz 4, with the example from NumPy documentation.

So, the preferred method to seed the sequence is the following:

from numpy.random import MT19937
from numpy.random import RandomState, SeedSequence

def seed(x):
    return RandomState(MT19937(SeedSequence(x)))

I still don't know what's MT19937, but from a practical POV, I think I'm satisfied. That's not to say that if you know more, you don't need to share... You're welcome to share. Thanks ;)

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.