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()
-0.16999453417038898

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)
14.030883542857888

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

rng.normal(15, 2, size=(8, 5))
array([[13.0261, 16.0387, 14.5391, 13.9578, 16.4479],
       [15.6777, 13.1057, 15.773 , 16.2885, 12.8012],
       [17.175 , 16.8414, 15.7467, 14.2084, 14.0483],
       [16.3881, 15.3198, 14.7789, 11.9975, 12.7277],
       [16.8451, 12.4137, 15.4169, 15.4382, 15.485 ],
       [11.7631, 12.9585, 13.1624, 14.723 , 13.7713],
       [15.3296, 13.7202, 14.3269, 12.2975, 16.2987],
       [20.3119, 14.8681, 11.6512, 16.2683, 15.9077]])

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.7088, -0.9209, -0.1081],
       [-0.2641, -0.5934, -0.34  ],
       [ 1.5949,  0.1625, -0.0247],
       [ 1.8571,  0.8568,  0.0631],
       [-0.9657,  0.6742,  0.2398]])

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]])