Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 935
Laurent Grégoire Avatar asked Jan 09 '12 09:01

Laurent Grégoire


People also ask

How does Python generate pseudorandom numbers?

Python uses the Mersenne Twister as the core generator. It produces 53-bit precision floats and has a period of 2**19937-1. The underlying implementation in C is both fast and threadsafe. The Mersenne Twister is one of the most extensively tested random number generators in existence.

Is Python random pseudo random?

Python Random module is an in-built module of Python which is used to generate random numbers. These are pseudo-random numbers means these are not truly random.

What is pseudorandom number Python?

The pseudorandom number generator is a mathematical function that generates a sequence of nearly random numbers. It takes a parameter to start off the sequence, called the seed. The function is deterministic, meaning given the same seed, it will produce the same sequence of numbers every time.

Why is seed 42?

It's a pop-culture reference! In Douglas Adams's popular 1979 science-fiction novel The Hitchhiker's Guide to the Galaxy, towards the end of the book, the supercomputer Deep Thought reveals that the answer to the great question of “life, the universe and everything” is 42.


1 Answers

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.

like image 180
Fred Foo Avatar answered Sep 28 '22 06:09

Fred Foo