Why random matters in analytics
Random values help with:
- Simulation and Monte Carlo experiments
- Creating synthetic datasets for testing
- Sampling
- Bootstrapping
numpy.random supplements Python's built-in random module with functions that generate whole arrays of samples at once. Python's random module only produces one value at a time, so for large samples NumPy is dramatically faster.
Recommended API: default_rng()
Modern NumPy recommends using a Generator object instead of the old global numpy.random functions:
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.
import numpy as np
rng = np.random.default_rng(123)
print(rng.normal(size=3))Random integers
import numpy as np
rng = np.random.default_rng(7)
arr = rng.integers(low=0, high=100, size=10)
print(arr)Random floats
import numpy as np
rng = np.random.default_rng(7)
arr = rng.random(5) # uniform in [0, 1)
print(arr)Normal distribution
import numpy as np
rng = np.random.default_rng(7)
arr = rng.normal(loc=0, scale=1, size=5)
print(arr)Choice (sampling)
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:
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
import numpy as np
rng = np.random.default_rng(7)
arr = np.arange(10)
rng.shuffle(arr)
print(arr)Synthetic dataset example
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)Check your understanding
Which function does the lesson recommend for modern random number generation?
default_rng() creates a Generator object, the approach NumPy now recommends over the old global functions.
Show answer
B โ np.random.default_rng() โ default_rng() creates a Generator object, the approach NumPy now recommends over the old global functions.
Why pass a seed such as default_rng(123)?
Seeding makes the stream reproducible, which matters for experiments and tutorials.
Show answer
B โ To get the same random results on every run โ Seeding makes the stream reproducible, which matters for experiments and tutorials.
What does rng.random(5) return?
rng.random draws uniform floats from 0 up to but not including 1. rng.normal draws from a normal distribution.
Show answer
B โ Five floats uniform in [0, 1) โ rng.random draws uniform floats from 0 up to but not including 1. rng.normal draws from a normal distribution.
In rng.choice(values, size=10, p=probs) with probs = [0.6, 0.3, 0.1], what does p control?
p gives the weight of each option, so low is picked about 60 percent of the time in the lesson example.
Show answer
B โ The probability of picking each value โ p gives the weight of each option, so low is picked about 60 percent of the time in the lesson example.
Next
Continue to: Linear Algebra with NumPy to learn dot products, matrix multiplication, and solving systems.
๐งช Try It Yourself
Exercise 1 โ A Seeded Generator
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 2 โ Weighted Sampling
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.Exercise 3 โ Shuffle In Place
Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.
Press Run to see what your code prints.