Reproducibility of python pseudo-random numbers across systems and versions?

我们两清 提交于 2019-12-01 13:56:53

问题


I need to generate a controlled sequence of pseudo-random numbers, given an initial parameter. For that I'm using the standard python random generator, seeded by this parameter. I'd like to make sure that I will generate the same sequence across systems (Operating system, but also Python version).

In summary: Does python ensure the reproducibility / portability of it's pseudo-random number generator across implementation and versions?


回答1:


No, it doesn't. There's no such promise in the random module's documentation.

What the docs do contain is this remark:

Changed in version 2.3: MersenneTwister replaced Wichmann-Hill as the default generator

So a different RNG was used prior to Python 2.3.

So far, I've been using numpy.random.RandomState for reproducible pseudo-randomness, though it too does not make the formal promise you're after.

If you want full reproducibility, you might want to include a copy of random's source in your program, or hack together a "P²RNG" (pseudo-pseudo-RNG) from hashlib.




回答2:


Not necessarily.

As described in the documentation, the random module has used the Mersenne twister to generate random numbers since version 2.3, but used Wichmann-Hill before that.

(If a seed is not provided, the method of obtaining the seed also does depend on the operating system, the Python version, and factors such as the system time).




回答3:


@reubano - 3.2 changed the integer functions in random, to produce more evenly distributed (which inevitably means different) output.

That change was discussed in Issue9025, where the team discuss whether they have an obligation to stick to the previous output, even when it was defective. They conclude that they do not. The docs for the module guarantee consistency for random.random() - one might assume that the functions which call it (like random.randrange()) are implicitly covered under that guarantee, but that doesn't seem to be the case.




回答4:


Just as a heads up: in addition to the 2.3 change, python 3 gives numbers from python 2.x from randrange and probably other functions, even if the numbers from random.random are similar.



来源:https://stackoverflow.com/questions/8786084/reproducibility-of-python-pseudo-random-numbers-across-systems-and-versions

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