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?


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.


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


In the beginning of your application call random.seed(x) making sure x is always the same. This will ensure the sequence of pseudo random numbers will be the same during each run of the application.


Jon Clements pretty much answers my question. However it wasn't the real problem: It turns out, that the reason for my code's randomness was the numpy.linalg SVD because it does not always produce the same results for badly conditioned matrices !!

So be sure to check for that in your code, if you have the same problems!


Building on previous answers: be aware that many constructs can diverge execution paths, even when all seeds are controlled.

I was thinking "well I set my seeds so they're always the same, and I have no changing/external dependencies, therefore the execution path of my code should always be the same", but that's wrong.

The example that bit me was list(set(...)), where the resulting order may differ.