How Does Your Computer Generate Random Numbers?

What you should know about numpy and pseudo random number generators (PRNG).

Tanguy Marchand
Sicara's blog
2 min readJan 28, 2019

--

Read the original article on Sicara’s website here.

The Mersenne-Twister is a pseudo random number generator (PRNG) used by the numpy library. This article explains all you need to know before using it.

As a data scientist, I need to generate random numbers for my algorithms. It puzzled me to know that deterministic processes in my computer could generate random sequences. I had a deeper look into it, and here is what I’ve learned:

  • Your computer does not generate truly random numbers but uses algorithm such as the Mersenne-Twister to get pseudo random sequences.
  • It is important to know when and how the seed of your pseudo random generator is set, otherwise you might have bad surprises, especially when developing multiprocess applications.
  • Standard random generators are highly predictable and should not be used for cryptographic or game gambling purposes.

Generating random numbers with numpy

With numpy, the quickest way to obtain random numbers between 0 and 1 is to use the following:

A first random number: 0.2332029758567754
A second random number: 0.7277750980801885

Or (almost) equivalently:

A first random number: 0.8492693008307766
A second random number: 0.9858307170084044

In both ways, we are using what we call a pseudo random number generator or PRNG. Indeed, whenever we call a python function, such as np.random.rand() the output can only be deterministic and cannot be truly random. Hence, numpy has to come up with a trick to generate sequences of numbers that look like random and behave as if they came from a purely random source, and this is what PRNG are.

Mersenne-Twister: the PRNG you may have used dozens of times without knowing it!

The most spread PRNG is the Mersenne Twister (MT) generator that was designed in 1997 by M. Matsumoto and T. Nishimura. It works schematically as the following:

  • Step 1: We initialize our random generator with random_state= np.random.RandomState() and we generate an internal state of 624 integers.

Read the full article on Sicara’s website here.

--

--