How to always generate the same random numbers with numpy ?

Published: February 21, 2023

Tags: Python; Numpy;

DMCA.com Protection Status

Numpy can be used to generate random numbers that are repeatable. This is useful for many applications in data science and statistical analysis, where it is essential to have consistent results across multiple trials. To achieve this, a pseudo-random number generator (PRNG) should be used with a fixed seed value. All subsequent calculations will produce the same result, as long as the same seed value is used. Examples:

Using numpy random.seed()

Using numpy for pseudo-random number generation (PRNG) involves two steps: first, set the seed value and then generate random numbers. The seed value can be generated in various ways, such as using a randomly generated integer or by providing a user-defined integer. To set the seed value, numpy's random.seed() method should be used, passing in the desired seed value as an argument:

np.random.seed(42)

Note that the number 42 is a popular choice as a seed for generating random numbers, which pays homage to Douglas Adams' iconic novel The Hitchhiker's Guide to the Galaxy.

Once the random number generator has been initialized with a specific seed value, random numbers can be easily created using numpy.random.rand() or numpy.random.randn() methods. Examples:.

Examples of numpy random number generators

Using np.random.rand()

import numpy as np

np.random.seed(42)

np.random.rand()

output:

0.3745401188473625

Another example

import numpy as np

np.random.seed(42)

[ np.random.rand() for i in range(10)]

output:

[0.3745401188473625,
 0.9507143064099162,
 0.7319939418114051,
 0.5986584841970366,
 0.15601864044243652,
 0.15599452033620265,
 0.05808361216819946,
 0.8661761457749352,
 0.6011150117432088,
 0.7080725777960455]

Using numpy random_sample()

Let's generate a matrix of random floats with numpy using random_sample(:)

import numpy as np

np.random.seed(42)

n = 10

data = np.random.random_sample(n)

It will always generate the same random numbers:

[0.37454012 0.95071431 0.73199394 0.59865848 0.15601864 0.15599452
 0.05808361 0.86617615 0.60111501 0.70807258]

By using a fixed seed value, numpy can be used to generate repeatable pseudo-random numbers. This is useful for many data science and statistical analysis applications, as it ensures that the same results are obtained across multiple trials. Furthermore, this approach makes it easier to debug any errors or inconsistencies in the results.

References