set random seed programwide in python

PythonRandomSeed

Python Problem Overview


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?

Python Solutions


Solution 1 - 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.

Solution 2 - Python

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

Solution 3 - Python

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.

Solution 4 - Python

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!

Solution 5 - Python

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.

Solution 6 - Python

One important caveat is that for python versions earlier than 3.7, Dictionary keys are not deterministic. This can lead to randomness in the program or even a different order in which the random numbers are generated and therefore non-deterministic random numbers. Conclusion update python.

Solution 7 - Python

I was also puzzled by the question when reproducing a deep learning project.So I do a toy experiment and share the results with you.

I create two files in a project, which are named test1.py and test2.py respectively. In test1, I set random.seed(10) for the random module and print 10 random numbers for several times. As you can verify, the results are always the same.

What about test2? I do the same way except setting the seed for the random module.The results display differently every time. Howerver, as long as I import test1———even without using it, the results appear the same as in test1.

So the experiment comes the conclusion that if you want to set seed for all files in a project, you need to import the file/module that define and set the seed.

Solution 8 - Python

According to Jon's answer, setting random.seed(n), at the beginning of the main program will set the seed globally. Afterward to set seeds of the imported libraries, one can use the output from random.random(). For example,

rng = np.random.default_rng(int(abs(math.log(random.random()))))

tf.random.set_seed(int(abs(math.log(random.random()))))

Solution 9 - Python

You can guarantee this pretty easily by using your own random number generator.

Just pick three largish primes (assuming this isn't a cryptography application), and plug them into a, b and c: a = ((a * b) % c) This gives a feedback system that produces pretty random data. Note that not all primes work equally well, but if you're just doing a simulation, it shouldn't matter - all you really need for most simulations is a jumble of numbers with a pattern (pseudo-random, remember) complex enough that it doesn't match up in some way with your application.

Knuth talks about this.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMischa ObrechtView Question on Stackoverflow
Solution 1 - PythonJon ClementsView Answer on Stackoverflow
Solution 2 - PythonSida ZhouView Answer on Stackoverflow
Solution 3 - PythonChimeraView Answer on Stackoverflow
Solution 4 - PythonMischa ObrechtView Answer on Stackoverflow
Solution 5 - PythonJBSnorroView Answer on Stackoverflow
Solution 6 - PythonDavoud Taghawi-NejadView Answer on Stackoverflow
Solution 7 - PythonGaryView Answer on Stackoverflow
Solution 8 - PythonacciptrisView Answer on Stackoverflow
Solution 9 - Pythonuser1277476View Answer on Stackoverflow