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.
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.
Python number method seed() sets the integer starting value used in generating random numbers. Call this function before calling any other random module function.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With