Numpy random number generators#

#: standard imports
import numpy as np
# print arrays to 4 decimal places
np.set_printoptions(precision=4, suppress=True)

We often need random numbers, for tests and for taking random samples, and for other things. np.random is a submodule within numpy:

type(np.random)
module

It contains function that will create a random number generator.

# Make a random number generator.
rng = np.random.default_rng()
type(rng)
numpy.random._generator.Generator

This generator is an object that has a set of methods for returning random numbers of various sorts. For example, to return a single random number from the default normal distribution (mean 0, variance 1):

rng.normal()
-1.1669820428237623

You can set the mean and variance with the first two input parameters:

# Random number from distribution with mean 15, variance 2
rng.normal(15, 2)
15.259930604825799

To return a 8 by 5 array of random numbers from the same distribution:

rng.normal(15, 2, size=(8, 5))
array([[16.2527, 15.346 , 15.8599, 12.5622, 13.5262],
       [14.6236, 12.5836, 10.498 , 13.1337, 13.9984],
       [12.0577, 15.2835, 17.408 , 16.2504, 15.3106],
       [12.2391, 18.6478, 13.0608, 17.5234, 17.2435],
       [12.851 , 13.2269, 13.1495, 15.0674, 15.0543],
       [14.7015, 12.4364, 16.7728, 18.7806, 12.3986],
       [15.3668, 14.2383, 13.3124, 16.3343, 13.4796],
       [14.08  , 14.405 , 15.4209, 17.6106, 15.0758]])

A 5 by 3 array of random numbers from the standard normal distribution with mean 1 and variance 1:

rng.normal(size=(5, 3))
array([[-0.0792,  0.7533, -0.2878],
       [-1.3921, -1.4665,  0.8089],
       [-2.1497, -0.7667, -0.6054],
       [-0.0269, -0.3493,  0.0697],
       [-0.5209,  0.6685,  0.0655]])

Making random numbers predictable#

Sometimes you want to make sure that the random numbers are predictable, in that you will always get the same set of random numbers from a series of calls to the rng methods. You can achieve this by giving the random number generator a seed when you create it. This is an integer that sets the random number generator into a predictable state, such that it will always return the same sequence of random numbers from this point:

# Set the state of the random number generator on creation.
new_rng = np.random.default_rng(seed=42)
# One set of random numbers
first_random_arr = new_rng.normal(size=(4, 2))
first_random_arr
array([[ 0.3047, -1.04  ],
       [ 0.7505,  0.9406],
       [-1.951 , -1.3022],
       [ 0.1278, -0.3162]])
# Another set
second_random_arr = new_rng.normal(size=(4, 2))
second_random_arr
array([[-0.0168, -0.853 ],
       [ 0.8794,  0.7778],
       [ 0.066 ,  1.1272],
       [ 0.4675, -0.8593]])
# Make another random number generator with the same seed.
new_rng2 = np.random.default_rng(seed=42)
# The same as "first_random_arr" above.
new_rng2.normal(size=(4, 2))
array([[ 0.3047, -1.04  ],
       [ 0.7505,  0.9406],
       [-1.951 , -1.3022],
       [ 0.1278, -0.3162]])
# The same as "second_random_arr" above.
new_rng2.normal(size=(4, 2))
array([[-0.0168, -0.853 ],
       [ 0.8794,  0.7778],
       [ 0.066 ,  1.1272],
       [ 0.4675, -0.8593]])