Random numbers

NumPy provides a wide range of functions to generate random numbers in arrays. These functions are available in the numpy.random module.

The random numbers are generated using the same, excellent pseudo-random number generator called Mersenne Twister that is also used in the normal random module. Mersenne Twister has a period of 2^19937-1 and is generally regarded as a very good pseudo-random number generator (for non-cryptographic purposes).

Several functions for constructing random arrays are provided, including:

  • random: uniform random numbers
  • normal: normal distribution
  • choice: random sample from given array
a = numpy.random.random((2,2))

# output:
#   [[ 0.02909142  0.90848 ]
#    [ 0.9471314   0.31424393]]

b = numpy.random.choice(numpy.arange(4), 10)

# output: [0 1 1 2 1 1 2 0 2 3]

What kind of random numbers do you need in your won work? Can you find them in NumPy?

Share this article:

This article is from the free online course:

Python in High Performance Computing

Partnership for Advanced Computing in Europe (PRACE)