Setting random seed in NumPy (4 examples)

Updated: March 1, 2024 By: Guest Contributor Post a comment

NumPy is a fundamental package for numerical computing in Python. Its capabilities in array computing are essential for scientific computing applications. An important concept when working with random numbers in NumPy (or any other computational tool) is the notion of a seed. This tutorial will guide you through the process of setting a random seed in NumPy through four progressive examples. Understanding how to control randomness can be crucial in many contexts such as reproducible research, testing, and simulation.

Introduction to Randomness and Seed

Random numbers are a cornerstone in various computing applications, but true randomness is difficult to achieve. Most random numbers used in software are actually pseudo-random, meaning they are generated in a deterministic way by an algorithm. The starting point for these pseudo-random number sequences is called the ‘seed’. Setting the same seed value will generate the same sequence of numbers, ensuring reproducibility of results that rely on random number generation.

NumPy provides a straightforward interface to work with randomness through its np.random module. By understanding how to set and use the random seed, users can achieve consistent outcomes in simulations or data analyses that involve random number generation.

Example 1: Basic Seed Setting

To start off, let’s look at the most basic way of setting a seed in NumPy. This example lays the foundation for understanding how seeding affects the generation of random numbers.

import numpy as np

# Set the seed	np.random.seed(0)

# Generate five random numbers	rand_nums = np.random.rand(5)
print(rand_nums)

Output:

[0.5488135  0.71518937 0.60276338 0.54488318 0.4236548 ]

In this example, we used np.random.seed(0) to set the seed, and then generated five random numbers. No matter how many times you run this code, as long as the seed value is 0, the generated numbers will be the same.

Example 2: Generating Reproducible Random Arrays

Building on the basic example, let’s generate larger arrays of random numbers in a reproducible manner. This example demonstrates how setting the seed ensures the same random array is generated each time the code is executed.

import numpy as np

# Set the seed	np.random.seed(42)

# Generate a 2x3 array of random numbers
random_array = np.random.rand(2, 3)
print(random_array)

Output:

[[0.37454012 0.95071431 0.73199394]
 [0.59865848 0.15601864 0.15599452]]

This illustrates how the same 2×3 array of random numbers is reproduced each time, provided the same seed (in this case, 42) is used.

Example 3: Seed with Random Integer Generation

Next, let’s explore generating random integers within a specific range, another common requirement. This example shows how seeding effects the generation of random integers.

import numpy as np

# Set the seed
np.random.seed(7)

# Generate five random integers between 1 and 10
rand_integers = np.random.randint(1, 11, size=5)
print(rand_integers)

Output:

[5 9 9 2 9]

As seen, setting the seed to 7 results in the same sequence of five random integers between 1 and 10 each time this block is executed.

Example 4: Advanced Use – Random Seeds in Simulations

Moving towards more sophisticated applications, we consider how setting a seed plays a critical role in simulations or modeling that involve stochastic processes. In this example, we’ll run a small simulation that requires the generation of random numbers at different steps, but still yields reproducible results.

import numpy as np

# Set seed
np.random.seed(12345)

# A direct simulation
simulation_results = []
for _ in range(5):
    simulation_results.append(np.random.random())

print(simulation_results)

Output:

[0.92961609 0.31637555 0.18391881 0.20456028 0.56772503]

This illustrates the fundamental role a fixed seed plays in ensuring that simulations yield the same results in successive runs, which is vital for the integrity and reproducibility of scientific research.

Conclusion

Understanding how and why to set a random seed in NumPy is fundamental for anyone involved in scientific computing and simulations. Through the four examples provided, we’ve seen how simple commands control the chaos inherent in randomness, ensuring reproducibility and reliability in computational research. Whether you’re a data scientist, engineer, or researcher, mastering this aspect of NumPy can significantly enhance the quality and trustworthiness of your work.