Random Numbers in NumPy
🎲 Random Numbers in NumPy (Complete Guide)
NumPy provides a powerful random module (numpy.random) to generate random numbers, arrays, choices, and distributions.
✅ 1. Generate a Single Random Number
👉 Generates a random float between 0 and 1.
✅ 2. Generate Random Integer
👉 Generates a random integer between 0 and 9.
With Range:
✅ 3. Generate Random Array
1D Array
2D Array
✅ 4. Generate Random Float Array
👉 Returns random floats between 0 and 1.
🎯 Random Choice (Pick Random Value from List)
Multiple Random Choices
🔄 Random Choice with Output Shape
🎯 Random Distribution
NumPy supports many probability distributions.
Example: Normal Distribution
| Parameter | Meaning |
|---|---|
loc |
Mean |
scale |
Standard deviation |
size |
Shape of output |
2D Normal Distribution
📦 Random Shuffle
Shuffle array elements in place
📦 Random Permutation (Creates Copy)
🔁 Seeding — Same Random Output Every Time
Useful in machine learning experiments to ensure reproducibility.
🧠 Summary Table
| Function | Purpose |
|---|---|
random.rand() |
Random float (0–1) |
random.randint() |
Random integer |
random.random() |
Random floats array |
random.choice() |
Pick random element(s) |
random.normal() |
Normal distribution |
random.shuffle() |
Shuffle original array |
random.permutation() |
Shuffled copy |
random.seed() |
Fix random results |
