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.34226728081257085

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.57283980956615

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

rng.normal(15, 2, size=(8, 5))
array([[12.6106, 15.1636, 12.9252, 13.8115, 14.0583],
       [ 7.6281, 17.1641, 14.0648, 12.1612, 16.4149],
       [15.4589, 15.7429, 15.3513, 14.5002, 16.4679],
       [14.0262, 13.3321, 18.0072, 17.213 , 15.3075],
       [15.2396, 14.2613, 15.1604, 15.1503, 14.811 ],
       [11.9148, 16.5794, 14.5032, 17.9572, 14.4331],
       [13.0585, 17.0516, 12.121 , 17.6757, 11.4749],
       [18.4293, 12.2325, 18.3925, 16.5487, 15.9709]])

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.9556, -2.6117,  1.5814],
       [-1.1922, -0.2481,  1.6778],
       [-1.8005,  0.9453, -1.4027],
       [ 1.8498,  0.1025, -1.0869],
       [-0.182 ,  0.2813, -2.5928]])

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