Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set random seed programwide in python

I have a rather big program, where I use functions from the random module in different files. I would like to be able to set the random seed once, at one place, to make the program always return the same results. Can that even be achieved in python?

like image 848
Mischa Obrecht Avatar asked Jul 17 '12 16:07

Mischa Obrecht


People also ask

How do you assign a random seed value in Python?

Python Random seed() Method The random number generator needs a number to start with (a seed value), to be able to generate a random number. By default the random number generator uses the current system time. Use the seed() method to customize the start number of the random number generator.

What seed does Python random use?

random. seed(a, version) in python is used to initialize the pseudo-random number generator (PRNG). PRNG is algorithm that generates sequence of numbers approximating the properties of random numbers. These random numbers can be reproduced using the seed value.

What is NumPy random seed in Python?

NumPy random seed is for pseudo-random numbers in Python. So what exactly is NumPy random seed? NumPy random seed is simply a function that sets the random seed of the NumPy pseudo-random number generator. It provides an essential input that enables NumPy to generate pseudo-random numbers for random processes.


Video Answer


2 Answers

The main python module that is run should import random and call random.seed(n) - this is shared between all other imports of random as long as somewhere else doesn't reset the seed.

like image 92
Jon Clements Avatar answered Oct 03 '22 23:10

Jon Clements


zss's comment should be highlighted as an actual answer:

Another thing for people to be careful of: if you're using numpy.random, then you need to use numpy.random.seed() to set the seed. Using random.seed() will not set the seed for random numbers generated from numpy.random. This confused me for a while. -zss

like image 31
Sida Zhou Avatar answered Oct 03 '22 23:10

Sida Zhou