Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python random.setstate(), seed() - is there guarantee of same results across implementations?

Is there guarantee that pyhon2/python3 script with random generator initialized with random.setstate() or random.seed() will produce same sequence of pseudo-randomness across different versions and platforms ? (for example python 3.1 on Mac , the same as python 3.2 on Linux 64-bit)

Question is about both: python2 and python3, with assumption that python3 scripts will run on python3 interpreters and vice versa.

like image 867
Grzegorz Wierzowiecki Avatar asked Sep 16 '12 19:09

Grzegorz Wierzowiecki


People also ask

What does random seed () do in Python?

Python Random seed() Method The seed() method is used to initialize the random number generator. 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.

What is set seed in Python?

Python number method seed() sets the integer starting value used in generating random numbers. Call this function before calling any other random module function.

How do you generate the same random number in Python?

random seed() example to generate the same random number every time. If you want to generate the same number every time, you need to pass the same seed value before calling any other random module function. Let's see how to set seed in Python pseudo-random number generator.

What is a seed in random number generator?

When you use statistical software to generate random numbers, you usually have an option to specify a random number seed. A seed is a positive integer that initializes a random-number generator (technically, a pseudorandom-number generator). A seed enables you to create reproducible streams of random numbers.


2 Answers

Python 2.3 and up use the Mersenne Twister generator, which is independent of the system random function (implemented as a C extension module for Python). For any version using the Mersenne Twister, the results should be the same across versions and platforms.

Previously, you could guarantee backwards compatibility using the WichmannHill generator, but unfortunately it seems that has been removed in Python 3.x.

If you absolutely need to guarantee compatibility, write your own Random subclass (or use a stable external implementation, e.g. simplerandom) as recommended by the random documentation:

Class Random can also be subclassed if you want to use a different basic generator of your own devising: in that case, override the random(), seed(), getstate(), setstate() and jumpahead() methods. Optionally, a new generator can supply a getrandbits() method — this allows randrange() to produce selections over an arbitrarily large range.

like image 64
nneonneo Avatar answered Nov 09 '22 06:11

nneonneo


You could use the simplerandom module, which has a consistent implementation independent of the Python platform. It has support for Python 2.4, 2.5, 2.6, 2.7, 3.1, and 3.2. It has 9 different algorithms.

Here's an example:

>>> import simplerandom.iterators as sri
>>> rng = sri.MWC1(12345)
>>> next(rng)
498186671L
>>> next(rng)
888940288L
>>> next(rng)
345072384L

And as long as you seed with the same value, you get the same results:

>>> rng = sri.MWC1(12345)
>>> next(rng)
498186671L
>>> rng = sri.MWC1(98765)
>>> next(rng)
3546724783L
like image 25
jterrace Avatar answered Nov 09 '22 06:11

jterrace