Random Numbers in NumPy

🎲 Random Numbers in NumPy

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


ParameterMeaning
locMean
scaleStandard deviation
sizeShape 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

FunctionPurpose
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

🎯 Practice Tasks

# 1. Create a 4x4 array of random integers between 1–100
# 2. Shuffle the array
# 3. Generate 10 random even numbers between 50–100
# 4. Create a normal distribution with mean=500, std=50, size=1000

You may also like...