NumPy Random Module
Why random matters in analytics
Random values help with:
- Simulation and Monte Carlo experiments
- Creating synthetic datasets for testing
- Sampling
- Bootstrapping
numpy.randomnumpy.random supplements Python’s built-in randomrandom module with functions that generate whole arrays of samples at once. Python’s randomrandom module only produces one value at a time, so for large samples NumPy is dramatically faster.
Recommended API: default_rng()default_rng()
Modern NumPy recommends using a Generator object instead of the old global numpy.randomnumpy.random functions:
rng
import numpy as np
rng = np.random.default_rng(42)
print(rng.integers(1, 10, size=5))rng
import numpy as np
rng = np.random.default_rng(42)
print(rng.integers(1, 10, size=5))flowchart LR A["np.random.default_rng(seed)"] --> B["Generator object (rng)"] B --> C["rng.integers(...)"] B --> D["rng.normal(...)"] B --> E["rng.choice(...)"] B --> F["rng.shuffle(...)"] C --> G["Same seed -> same numbers every run"] D --> G E --> G
Seeding (reproducibility)
Seeding ensures you get the same results each run — essential for reproducible experiments and tutorials.
seed
import numpy as np
rng = np.random.default_rng(123)
print(rng.normal(size=3))seed
import numpy as np
rng = np.random.default_rng(123)
print(rng.normal(size=3))Random integers
integers
import numpy as np
rng = np.random.default_rng(7)
arr = rng.integers(low=0, high=100, size=10)
print(arr)integers
import numpy as np
rng = np.random.default_rng(7)
arr = rng.integers(low=0, high=100, size=10)
print(arr)Random floats
random
import numpy as np
rng = np.random.default_rng(7)
arr = rng.random(5) # uniform in [0, 1)
print(arr)random
import numpy as np
rng = np.random.default_rng(7)
arr = rng.random(5) # uniform in [0, 1)
print(arr)Normal distribution
normal
import numpy as np
rng = np.random.default_rng(7)
arr = rng.normal(loc=0, scale=1, size=5)
print(arr)normal
import numpy as np
rng = np.random.default_rng(7)
arr = rng.normal(loc=0, scale=1, size=5)
print(arr)Choice (sampling)
choice
import numpy as np
rng = np.random.default_rng(7)
categories = np.array(["A", "B", "C"])
print(rng.choice(categories, size=10, replace=True))choice
import numpy as np
rng = np.random.default_rng(7)
categories = np.array(["A", "B", "C"])
print(rng.choice(categories, size=10, replace=True))Weighted sampling:
choice-weights
import numpy as np
rng = np.random.default_rng(7)
values = np.array(["low", "medium", "high"])
probs = [0.6, 0.3, 0.1]
print(rng.choice(values, size=10, p=probs))choice-weights
import numpy as np
rng = np.random.default_rng(7)
values = np.array(["low", "medium", "high"])
probs = [0.6, 0.3, 0.1]
print(rng.choice(values, size=10, p=probs))Shuffle
shuffle
import numpy as np
rng = np.random.default_rng(7)
arr = np.arange(10)
rng.shuffle(arr)
print(arr)shuffle
import numpy as np
rng = np.random.default_rng(7)
arr = np.arange(10)
rng.shuffle(arr)
print(arr)Synthetic dataset example
synthetic
import numpy as np
rng = np.random.default_rng(0)
n = 100
age = rng.integers(18, 60, size=n)
income = rng.normal(loc=60000, scale=15000, size=n)
X = np.column_stack([age, income])
print(X.shape)synthetic
import numpy as np
rng = np.random.default_rng(0)
n = 100
age = rng.integers(18, 60, size=n)
income = rng.normal(loc=60000, scale=15000, size=n)
X = np.column_stack([age, income])
print(X.shape)Next
Continue to: Linear Algebra with NumPy to learn dot products, matrix multiplication, and solving systems.
🧪 Try It Yourself
Exercise 1 – A Seeded Generator
Exercise 2 – Weighted Sampling
Exercise 3 – Shuffle In Place
If this helped you, consider buying me a coffee ☕
Buy me a coffeeWas this page helpful?
Let us know how we did
