set random seed programwide in python

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 20:59:37

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!

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!