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

However, when I tried running 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?

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 example, in order to do something similar to numpy.random.seed(constant)?

  • 1
    $\begingroup$ This seems like an artifact of running in Jupyter. If you put all of that in a .py file that you run from the command line, the seeding will remain throughout the entirety of the script. $\endgroup$
    – Dave
    Commented Nov 18, 2020 at 11:15
  • 1
    $\begingroup$ I think asking this in the main site of stack overflow will give you an answer much faster $\endgroup$
    – Gulzar
    Commented Nov 18, 2020 at 18:19
  • 1
    $\begingroup$ Is this the same issue: stackoverflow.com/questions/51424857/…? $\endgroup$
    – Burger
    Commented Nov 18, 2020 at 19:56


Browse other questions tagged or ask your own question.