NumPy Random Number Generation

Carson West

Libraries like NumPy

NumPy Random Number Generation

NumPy’s random module (now deprecated; use numpy.random directly) provides functions for generating various random numbers and distributions. It’s significantly faster than Python’s built-in random module, especially for large arrays.

Key Functions & Concepts:

import numpy as np

# 3x3 array of random floats between 0 and 1
random_array = np.random.rand(3, 3)
print(random_array)
# 2x2 array of random floats from a standard normal distribution
normal_array = np.random.randn(2, 2)
print(normal_array)
# 5 random integers between 1 and 10 (inclusive)
random_integers = np.random.randint(1, 11, 5) # 11 is exclusive
print(random_integers)
# Sample 3 elements from an array with replacement
my_array = np.array(1, 2, 3, 4, 5)
sample = np.random.choice(my_array, size=3, replace=True)
print(sample)


# Sample with probabilities
probabilities = np.array([0.1, 0.2, 0.3, 0.2, 0.2) # must sum to 1
weighted_sample = np.random.choice(my_array, size=3, replace=True, p=probabilities)
print(weighted_sample)
np.random.seed(42) # set seed to 42
print(np.random.rand(2))
np.random.seed(42) # setting it again produces the same results
print(np.random.rand(2))

Deprecated random Module:

The top-level numpy.random module is now preferred over the older np.random module. While the older module might still work in some cases, it is recommended to transition to the newer approach for better compatibility and future-proofing.

Note: For more advanced random number generation and statistical simulations, consider using libraries like SciPy.(SciPy Statistical Functions)