Navigation

Python

How to Use NumPy Random Number Generation

Generate random numbers, arrays, and samples with NumPy's powerful random module - from simple randoms to complex statistical distributions.

Table Of Contents

Randomness Under Control

Whether you need test data, Monte Carlo simulations, or machine learning datasets, NumPy's random module is your gateway to controlled chaos.

Random Number Generation Essentials

import numpy as np

# Modern way: use Generator (recommended)
rng = np.random.default_rng(seed=42)  # Reproducible results

# Basic random generation
print(rng.random())                    # Single float [0, 1)
print(rng.random(5))                   # Array of 5 floats
print(rng.integers(1, 10, size=5))     # Integers between 1-9

# Common distributions
print(rng.normal(0, 1, 100))           # Normal distribution (mean=0, std=1)
print(rng.uniform(-1, 1, 50))          # Uniform distribution [-1, 1)
print(rng.exponential(2.0, 30))        # Exponential distribution

# Random sampling
data = ['apple', 'banana', 'cherry', 'date']
print(rng.choice(data, 3))             # Sample 3 items with replacement
print(rng.choice(data, 2, replace=False))  # Without replacement

# Random array operations
arr = np.arange(10)
rng.shuffle(arr)                       # Shuffle in-place
print(arr)

# Random permutation
permuted = rng.permutation(10)         # New shuffled array
print(permuted)

Statistical Distributions Gallery

rng = np.random.default_rng(42)

# Discrete distributions
binomial = rng.binomial(10, 0.3, 100)      # Binomial (n=10, p=0.3)
poisson = rng.poisson(2.5, 100)            # Poisson (λ=2.5)

# Continuous distributions
gaussian = rng.normal(100, 15, 1000)       # Normal (μ=100, σ=15)
chi2 = rng.chisquare(3, 100)              # Chi-square (df=3)
beta = rng.beta(2, 5, 100)                # Beta distribution

# Custom probability weights
values = [1, 2, 3, 4, 5]
weights = [0.1, 0.2, 0.4, 0.2, 0.1]
weighted_sample = rng.choice(values, 100, p=weights)

Seeded Randomness for Reproducibility

# Reproducible experiments
def generate_data(seed=None):
    rng = np.random.default_rng(seed)
    return rng.normal(0, 1, 1000)

# Same seed = same results
data1 = generate_data(42)
data2 = generate_data(42)
print(np.array_equal(data1, data2))  # True

# Different runs with different seeds
results = [generate_data() for _ in range(3)]  # Different each time

Advanced Random Techniques

rng = np.random.default_rng()

# Random matrix generation
random_matrix = rng.random((3, 4))         # 3x4 random matrix
identity_noise = np.eye(3) + rng.normal(0, 0.1, (3, 3))

# Bootstrap sampling
original_data = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
bootstrap_samples = [rng.choice(original_data, size=len(original_data), 
                                replace=True) for _ in range(1000)]

# Random walks
steps = rng.choice([-1, 1], 1000)
walk = np.cumsum(steps)

Legacy vs Modern API

# Old way (still works but not recommended)
np.random.seed(42)
old_random = np.random.random(5)

# New way (recommended)
rng = np.random.default_rng(42)
new_random = rng.random(5)

# Both produce the same results, but Generator is more flexible

Performance Tips

  • Use Generator for better performance and more features
  • Generate large arrays at once instead of in loops
  • Consider using specific distributions instead of transforming uniform random
  • Set seeds for reproducible experiments

Explore Further

Master statistical analysis with NumPy, dive into Monte Carlo methods, and explore machine learning data generation.

Share this article

Add Comment

No comments yet. Be the first to comment!

More from Python