🎲 NumPy Random Number Generation
The Magic Dice Factory
Imagine you have a magical dice factory. Every time you press a button, it creates dice that roll to whatever numbers you need. Sometimes you want random numbers between 1 and 6. Sometimes between 1 and 1000. This factory can make dice for ANY numbers you want!
NumPy’s random module is exactly like this magical dice factory.
🎰 Random Number Generators
What is a Random Number Generator?
Think of a Random Number Generator (RNG) like a machine that picks lottery numbers. Every time you ask, it gives you a new surprise number!
import numpy as np
# Create your personal dice machine
rng = np.random.default_rng()
# Roll one dice (number between 0 and 1)
print(rng.random())
# Output: 0.7382719...
The Old Way vs The New Way
NumPy has two ways to make random numbers:
| Old Way (Legacy) | New Way (Recommended) |
|---|---|
np.random.rand() |
rng.random() |
| Uses global state | Uses personal RNG |
| Less predictable | More control |
Always use the new way!
# NEW WAY - Create your own generator
rng = np.random.default_rng()
number = rng.random()
# OLD WAY - Don't use this anymore
old_number = np.random.rand()
📦 Generating Random Arrays
Making Many Dice at Once
Why roll one dice when you can roll hundreds? NumPy can create entire arrays of random numbers instantly!
rng = np.random.default_rng()
# 5 random numbers between 0 and 1
five_randoms = rng.random(5)
print(five_randoms)
# [0.23, 0.87, 0.12, 0.56, 0.91]
# 3x3 grid of random numbers
grid = rng.random((3, 3))
print(grid)
# [[0.2, 0.5, 0.8],
# [0.1, 0.9, 0.3],
# [0.6, 0.4, 0.7]]
Random Integers - Whole Numbers Only
Sometimes you need whole numbers, like dice that show 1, 2, 3, 4, 5, or 6.
rng = np.random.default_rng()
# Roll 10 dice (1 to 6)
dice_rolls = rng.integers(1, 7, size=10)
print(dice_rolls)
# [3, 6, 1, 4, 2, 5, 3, 6, 1, 2]
# Note: 7 is excluded, so we get 1-6
Shape Magic
You can create random arrays in any shape!
rng = np.random.default_rng()
# 1D array: 5 numbers
line = rng.random(5)
# 2D array: 3 rows, 4 columns
table = rng.random((3, 4))
# 3D array: 2 layers, 3 rows, 4 cols
cube = rng.random((2, 3, 4))
🔐 Random Seed Reproducibility
The Time Machine for Random Numbers
Here’s a mind-bending idea: What if you could get the SAME “random” numbers every time you run your code?
That’s what seeds do!
Think of a seed like a secret password. Use the same password, get the same random numbers. Every. Single. Time.
# With seed = 42, you ALWAYS get same result
rng = np.random.default_rng(seed=42)
print(rng.random(3))
# [0.773..., 0.438..., 0.858...]
# Run again with same seed
rng = np.random.default_rng(seed=42)
print(rng.random(3))
# [0.773..., 0.438..., 0.858...]
# SAME NUMBERS!
Why Seeds Matter
graph TD A[Same Seed] --> B[Same Random Numbers] B --> C[Reproducible Results] C --> D[Scientists Can Verify] C --> E[Debug Your Code] C --> F[Share Experiments]
Real Example:
- Your friend runs your code in Japan
- You run it in Brazil
- Same seed = Same results
- Science can be checked!
📊 Random Sampling Distributions
Not All Randomness is Equal
Imagine throwing darts at a board. Sometimes you aim for the center (normal distribution). Sometimes you throw blindfolded (uniform distribution).
Uniform Distribution
Every number has an equal chance. Like a fair lottery.
rng = np.random.default_rng()
# Numbers between 0 and 1 (uniform)
uniform = rng.uniform(0, 1, size=5)
# [0.23, 0.67, 0.91, 0.12, 0.45]
# Numbers between 10 and 20
scaled = rng.uniform(10, 20, size=5)
# [12.3, 18.7, 11.2, 15.6, 19.1]
Normal Distribution (Bell Curve)
Most values cluster around the middle. Like people’s heights!
rng = np.random.default_rng()
# Average = 170cm, Spread = 10cm
heights = rng.normal(170, 10, size=1000)
# Most people: 160-180cm
# Few people: below 150 or above 190
graph TD A[Normal Distribution] --> B[Mean: The Center] A --> C[Std Dev: The Spread] B --> D[Most values here] C --> E[How wide the bell is]
Other Fun Distributions
| Distribution | Use Case | Example |
|---|---|---|
uniform |
Equal chances | Lottery numbers |
normal |
Natural variation | Heights, IQ |
exponential |
Wait times | Time between calls |
poisson |
Counting events | Emails per hour |
binomial |
Yes/No outcomes | Coin flips |
rng = np.random.default_rng()
# Coin flips: 10 flips, 50% heads
flips = rng.binomial(10, 0.5, size=5)
# [6, 4, 5, 7, 5] heads out of 10
🔀 Random Permutations
Shuffling the Deck
Permutations are about mixing things up. Like shuffling a deck of cards or rearranging a playlist.
Shuffle In Place
Change the original array directly:
rng = np.random.default_rng()
cards = np.array([1, 2, 3, 4, 5])
rng.shuffle(cards)
print(cards)
# [3, 1, 5, 2, 4] - shuffled!
Get a Shuffled Copy
Keep the original, get a new shuffled version:
rng = np.random.default_rng()
original = np.array([1, 2, 3, 4, 5])
shuffled = rng.permutation(original)
print(original) # [1, 2, 3, 4, 5] - unchanged!
print(shuffled) # [4, 2, 5, 1, 3] - new order!
Random Sampling (Pick Some Items)
Pick a few items randomly without repetition:
rng = np.random.default_rng()
lottery = np.array([1,2,3,4,5,6,7,8,9,10])
# Pick 3 winners (no repeats)
winners = rng.choice(lottery, size=3, replace=False)
print(winners)
# [7, 2, 9]
With replacement (same item can be picked again):
# Pick 5 with replacement
picks = rng.choice(lottery, size=5, replace=True)
# [3, 7, 3, 1, 9] - 3 appears twice!
🧠 Quick Summary
graph LR A[NumPy Random] --> B[Generate Numbers] A --> C[Control with Seeds] A --> D[Choose Distributions] A --> E[Shuffle & Sample] B --> B1[random - floats 0-1] B --> B2[integers - whole numbers] C --> C1[Same seed = Same numbers] D --> D1[uniform - equal chance] D --> D2[normal - bell curve] E --> E1[shuffle - mix in place] E --> E2[permutation - get copy] E --> E3[choice - pick items]
🚀 Pro Tips
-
Always create your own generator
rng = np.random.default_rng() -
Use seeds for reproducibility
rng = np.random.default_rng(seed=42) -
Shape matters! Pass tuples for multi-dimensional arrays
rng.random((3, 4)) # 3 rows, 4 columns -
Choose the right distribution for realistic data
-
Use
replace=Falsewhen you need unique selections
You now have the power to generate any random data you need! Go forth and roll those digital dice! 🎲✨