🎲 NumPy Random – Generate Random Numbers Like a Pro
🧲 Introduction – Why Learn NumPy Random?
Random numbers are everywhere—from data augmentation in machine learning to simulation models in finance and physics. NumPy’s random
module gives you tools to generate numbers from uniform, normal, binomial, and many more distributions—all with blazing speed and flexibility.
🎯 By the end of this guide, you’ll:
- Understand how NumPy generates random numbers
- Use
np.random
to create random integers, floats, and arrays - Generate samples from common distributions (normal, binomial, etc.)
- Set seeds for reproducibility in experiments
- Learn differences between the legacy and new Generator API
🌀 Step 1: Generate Random Integers
import numpy as np
rand_ints = np.random.randint(1, 10, size=5)
print(rand_ints)
🔍 Explanation:
randint(1, 10)
→ Generates integers from 1 to 9 (10 excluded).size=5
→ Returns an array of 5 values.
✅ Output:[3 7 2 1 9]
(results vary every run)
🌊 Step 2: Generate Random Floats (0.0 to 1.0)
rand_floats = np.random.rand(3)
print(rand_floats)
🔍 Explanation:
rand(3)
→ Generates 3 floats between 0.0 and 1.0- Returns uniformly distributed values
✅ Output:[0.66 0.23 0.91]
🧱 Step 3: Create Random Arrays (Multi-Dimensional)
matrix = np.random.randint(0, 100, size=(2, 3))
print(matrix)
🔍 Explanation:
size=(2, 3)
→ 2 rows, 3 columns- Values between 0 and 99
✅ Output:
[[42 87 10]
[23 5 99]]
🎯 Step 4: Sampling from Normal Distribution
normal_samples = np.random.normal(loc=0, scale=1, size=5)
print(normal_samples)
🔍 Explanation:
loc=0
→ Meanscale=1
→ Standard deviationsize=5
→ 5 random numbers from standard normal distribution
✅ Output:[ 0.6 -1.2 0.1 1.5 -0.4 ]
🧪 Step 5: Choose Random Elements from a List
choices = np.random.choice([10, 20, 30, 40], size=3)
print(choices)
🔍 Explanation:
- Randomly picks 3 values with replacement from the given list
✅ Output:[20 10 30]
🔁 Step 6: Shuffle Arrays with shuffle()
arr = np.array([1, 2, 3, 4, 5])
np.random.shuffle(arr)
print(arr)
🔍 Explanation:
- Shuffles the array in place
✅ Output:[3 1 5 2 4]
(order may vary)
🧠 Step 7: Reproducibility with np.random.seed()
np.random.seed(42)
print(np.random.rand(3))
🔍 Explanation:
seed(42)
fixes the random output.- Same numbers will be generated every time the code runs.
✅ Output:[0.37454012 0.95071431 0.73199394]
💡 Useful in ML experiments and reproducible research.
🔄 Step 8: Use the New Generator
API (Recommended)
rng = np.random.default_rng(seed=123)
print(rng.integers(1, 100, size=3))
🔍 Explanation:
default_rng()
is NumPy’s modern random API (recommended after NumPy 1.17+)integers()
is similar torandint()
✅ Output:[67 98 80]
(reproducible with same seed)
📊 Step 9: Common Random Distributions
Function | Description | Example Syntax |
---|---|---|
random.rand() | Uniform floats [0, 1) | np.random.rand(3) |
random.randint() | Random integers | np.random.randint(1, 10, size=5) |
random.normal() | Normal (Gaussian) distribution | np.random.normal(0, 1, 100) |
random.uniform() | Uniform distribution (custom range) | np.random.uniform(5, 10, size=3) |
random.binomial() | Binomial distribution | np.random.binomial(n=10, p=0.5, size=5) |
random.choice() | Random selection from a list | np.random.choice([1, 2, 3]) |
random.shuffle() | Shuffle elements in-place | np.random.shuffle(arr) |
default_rng().integers() | New API for random integers | rng.integers(1, 100, size=5) |
📌 Summary – Recap & Next Steps
Randomness in NumPy is used for simulations, data sampling, testing, and machine learning. You now have the tools to generate random numbers, use different distributions, shuffle data, and reproduce results with seeding.
🔍 Key Takeaways:
- Use
rand()
,randint()
, andchoice()
for general random data - Use
normal()
,uniform()
, orbinomial()
for statistical simulations - Use
shuffle()
andpermutation()
for array manipulation - Prefer
default_rng()
for reproducible and modern code
⚙️ Real-world relevance: Random generation is essential for tasks like model initialization, data bootstrapping, A/B testing, and Monte Carlo simulations.
❓ FAQs – NumPy Random
❓ What’s the difference between np.random
and default_rng()
?
✅ default_rng()
is NumPy’s recommended, modern API with better random state management.
❓ How do I get reproducible random values?
✅ Use:
np.random.seed(0) # or use default_rng(seed=...)
❓ How do I randomly pick from a list?
✅ Use:
np.random.choice([10, 20, 30])
❓ Can I get random numbers from a normal distribution?
✅ Yes:
np.random.normal(loc=0, scale=1, size=1000)
❓ Is NumPy’s random better than Python’s built-in random
?
✅ Yes! It’s faster, supports more distributions, and works with arrays.
Share Now :