1. np.random.rand()
rand() generates random floating-point numbers from: 0.0 ≤ value < 1.0.
Example 1: Generate one random number
Possible output:
Every execution can produce a different number.
Example 2: Generate 5 random numbers
Possible output:
Shape:
Output:
So this is a 1-D array containing 5 values.
Example 3: Generate 3 × 3 array
Possible output:
Shape: 3 rows × 3 columns.
Example 4: Generate 2 × 4 array
Possible output:
Example 5: Generate 3-D array
Shape:
Output:
Think of it as:
2 blocks → 3 rows → 4 columns.
2. Generating random numbers in a specific range
rand() itself only produces values between 0 and 1. But we can use mathematical operations.
Example 6: Random numbers between 0 and 100
Possible output:
Formula:
Example 7: Random numbers between 10 and 20
Use: low + (high - low) * random
Possible output:
Or simply:
Example 8: Random numbers between 50 and 100
Possible output:
Practical Examples
3. Practical Example — Student Marks
Suppose we want random marks between 0 and 100.
Possible output:
4. Practical Example — Random Percentage
5. Practical Example — Random Temperature
Suppose temperature should be between 20°C and 40°C:
Possible output:
6. Practical Example — Random Discount
Generate discounts between 5% and 30%:
7. Practical Example — Random Salary
Suppose salary should be between ₹20,000 and ₹50,000:
Possible output:
8. 2-D Practical Example — Student Marks
Generate marks for 5 students × 3 subjects:
Possible output:
Here: 5 → students, 3 → subjects, 100 → marks scale.
Advanced Usage
9. Convert to Integer
rand() generates floats. If you want integers:
Possible output:
randint() is usually better.
10. Generate Boolean Values
We can use a condition:
Possible output:
This can be useful for generating random yes/no conditions.
11. Generate 0 or 1
Possible output:
12. Using seed()
Normally, random values change every time.
The two results will normally be different. If we want repeatable results:
Run the same program again:
You get the same sequence.
- Machine learning
- Testing
- Debugging
- Student demonstrations
- Reproducible experiments
13. Important Shape Examples
np.random.rand()→ One valuenp.random.rand(5)→ 1-D arraynp.random.rand(3, 4)→ 2-D arraynp.random.rand(2, 3, 4)→ 3-D arraynp.random.rand(2, 3, 4, 5)→ 4-D array
Complete rand() Cheat Sheet
One important point
Don't confuse:np.random.rand(3, 4) with np.random.rand((3, 4))
❌ The second form is incorrect for
rand().rand() takes dimensions as separate arguments:
random() takes the shape as a tuple:
3. np.random.randn()
randn() generates random numbers according to the standard normal distribution.
The standard normal distribution has:
- Mean (μ) = 0
- Standard deviation (σ) = 1
- Values can be negative, zero, or positive
Example 1: Generate one random value
Possible output:
Another execution could give 1.284 or 0.052.
So unlike rand(), the result is not restricted to 0–1.
Example 2: Generate 5 values
Possible output:
Notice that both positive and negative values are possible.
Example 3: Generate a 3 × 3 matrix
Possible output:
Example 4: Generate a 2 × 4 matrix
Possible output:
Example 5: 3-D Array
Output:
Meaning: 2 blocks → 3 rows → 4 columns.
Example 6: Check Mean
Generate many values:
The result will be approximately 0.00. For example: 0.0023.
randn() follows a distribution whose theoretical mean is 0.
Example 7: Check Standard Deviation
Output will be approximately 1.0. For example: 1.0021.
Example 8: rand() vs randn()
rand()Possible: [0.23 0.71 0.45 0.91 0.18 ...]
Values are: 0 ≤ x < 1
randn()Possible: [-0.43 1.21 -0.72 0.15 -1.34 ...]
Values can be negative or positive.
rand()→ Uniform distribution between 0 and 1randn()→ Standard normal distribution (mean = 0, std = 1)
Generate Data with a Specific Mean
This is an important practical use.
value = mean + standard_deviation × random_valueIn Python:
mean + std * np.random.randn()
Example 9: Student Marks
Suppose student marks have Mean = 70 and Standard deviation = 10.
We can generate random marks approximately around 70:
Possible output:
Example 10: Student Height
Suppose average student height is 165 cm and standard deviation is 8 cm.
This creates values distributed around 165.
Example 11: Temperature
Suppose average temperature is Mean = 30°C and Standard deviation = 3°C.
Example 12: Salary Simulation
Suppose average salary is ₹50,000 and standard deviation is ₹8,000.
Example 13: 1000 Student Marks
Suppose we want to simulate marks with Mean = 65 and Standard deviation = 12.
This is a good example for explaining normal distribution.
Advanced Examples
Example 14: Using seed()
If you want the same random values every time:
Run the program again with np.random.seed(10) and you will get the same sequence.
Example 15: Find Positive and Negative Values
Here we are using Boolean indexing.
Example 16: Count Positive Values
Since the standard normal distribution is centered around zero, approximately half of the values should be positive.
Example 17: Convert to Integer
Possible output: [ 0 -1 0 1 0 -2 1 0 0 -1]
Example 18: Generate a Random Error
This is a nice real-world example. Suppose predicted values have random measurement errors:
You can add this error to some actual values:
This is conceptually useful in statistics and machine learning.
Most Important Pattern
If you remember only one thing about randn(), remember:
means: Generate random values from a standard normal distribution.
And:
means: Generate n random values approximately centered around the specified mean with the specified standard deviation.
For example:
means: Mean ≈ 100, Standard deviation ≈ 15, Number of values = 1000
rand() vs randn() — Final Comparison
| Feature | rand() |
randn() |
|---|---|---|
| Distribution | Uniform | Normal |
| Typical range | 0 to 1 | No fixed range |
| Negative values | ❌ | ✅ |
| Mean | 0.5 | 0 |
| Standard deviation | ≈ 0.289 | 1 |
| Example | rand(5) |
randn(5) |
4. np.random.randint()
randint() is used to generate random integer values.
Syntax
The most important rule
low is included, high is excluded.So:
np.random.randint(1, 10) can generate: 1, 2, 3, 4, 5, 6, 7, 8, 9 but never 10.
Example 1 — One random integer
Possible output: 7
Here np.random.randint(10) means: 0 ≤ x < 10. So possible values are 0 to 9.
Example 2 — Between 1 and 10
Possible output: 6 (Possible values: 1 to 9)
Example 3 — Between 1 and 100
Possible output: 73 (Possible values: 1 to 99)
Example 4 — Generate 5 random integers
Possible output: [23 87 45 12 76]
Here:1 → minimum100 → maximum exclusive5 → number of values
Example 5 — Generate 10 random integers
Possible output: [78 23 91 45 67 12 88 34 56 72]
Why 101? Because the upper limit is excluded. randint(1, 101) gives 1 to 100.
Example 6 — size as a tuple
You can generate a 2-D array.
Possible output:
Shape: 3 rows × 4 columns.
Example 7 — 3 × 3 Matrix
Possible output:
Example 8 — 3-D Array
Output: (2, 3, 4) (Meaning: 2 blocks, 3 rows, 4 columns).
Practical Examples
Example 9 — Student Marks
Generate marks for 10 students:
Possible output: [78 91 56 83 42 67 88 34 72 95]
Because: 0 ≤ marks ≤ 100. We use 101 because 101 is excluded.
Example 10 — Dice
A dice has values: 1 2 3 4 5 6. Generate one dice roll:
Generate 10 dice rolls:
Possible output: [3 6 2 1 4 5 3 6 2 4]
Example 11 — Coin Toss
We can represent: 0 → Tail, 1 → Head
Generate 10 tosses:
Possible output: [1 0 1 1 0 0 1 0 1 1]
Example 12 — Random Age
Generate random ages between 18 and 60:
Notice 61 is used to include 60.
Example 13 — Random Year
Generate years between 2000 and 2026:
Example 14 — Random Roll Numbers
Suppose roll numbers are between 1 and 50:
[12 45 12 27 18] is perfectly possible.
Example 15 — Random Product Quantity
Suppose an order can contain 1–20 items:
Example 16 — Random Price
Generate integer prices between ₹100 and ₹1000:
Example 17 — Random Password Digits
Generate a 6-digit numeric code:
To display as one number/string:
Possible output: 729148
Example 18 — Random Binary Values
Generate only 0 and 1:
Useful for: Binary classification examples, Boolean-like data, Machine-learning demonstrations.
Advanced Examples & Statistics
Example 19 — dtype
You can specify the integer type:
Output: int32
Example 20 — Using seed()
Running the same code again with the same seed gives the same sequence. Useful for Testing, Debugging, Machine learning, Classroom demonstrations.
Example 21 — Find Maximum and Minimum
Example 22 — Random Matrix and Statistics
This is a nice combination of random generation + NumPy array operations.
Example 23 — Generate a 5 × 5 Matrix
Possible output:
Example 24 — Random Numbers with low Only
means: 0 ≤ value < 5. So np.random.randint(5) and np.random.randint(0, 5) are equivalent in this basic case.
Most Important Syntax
- One value:
np.random.randint(10)→ 0–9 - Range:
np.random.randint(10, 20)→ 10–19 - Multiple values:
np.random.randint(10, 20, 5)→ 5 random integers - 2-D array:
np.random.randint(10, 20, size=(3, 4))→ 3 × 4 random integer matrix - 3-D array:
np.random.randint(10, 20, size=(2, 3, 4))→ 2 × 3 × 4 array
rand() vs randn() vs randint()
| Function | Data | Example |
|---|---|---|
rand() |
Float 0–1 | np.random.rand(5) |
randn() |
Normal distribution | np.random.randn(5) |
randint() |
Integer range | np.random.randint(1, 100, 5) |
One rule students must remember
np.random.randint(1, 10) means 1 ≤ value < 10.
10 is NOT included.
So if you want 1 to 10, write:
np.random.randint(1, 11)
5. np.random.random()
np.random.random() generates random floating-point numbers between 0 and 1.
Basic rule
0.0 ≤ value < 1.0So 1.0 is never included.
Example 1 — One random number
Possible output: 0.673421
Example 2 — Generate 5 random numbers
Possible output: [0.23 0.71 0.45 0.91 0.18]
Shape: (5,)
Example 3 — 3 × 3 array
Possible output:
np.random.random((3, 3))
Example 4 — 2 × 4 array
Example 5 — 3-D Array
Output: (2, 3, 4)
Generating values in a specific range
Example 6 — Random values between 0 and 100
random() itself produces 0–1. Multiply by 100:
Possible output: [23.45 78.21 54.67 91.23 12.89]
Example 7 — Random values between 10 and 20
Use the formula: low + (high - low) × random
Simplified:
Example 8 — Random values between 50 and 100
Practical Examples
Example 9 — Student Percentage
Generate random percentages for 10 students:
Example 10 — Student Marks
Generate marks between 0 and 100:
If you need integer marks, use:
But for integer marks, randint() is cleaner: marks = np.random.randint(0, 101, 5)
Example 11 — Temperature
Generate temperatures between 20°C and 40°C:
Example 12 — Discount
Generate discounts between 5% and 30%:
Example 13 — Salary
Generate random salary values between ₹20,000 and ₹50,000:
Example 14 — 2-D Student Marks
Suppose: 5 students, 3 subjects, marks from 0–100:
Example 15 — Boolean Values
We can use a condition:
This gives approximately a 50/50 split.
Example 16 — Generate 0 and 1
Convert the Boolean values to integers:
Possible output: [1 0 1 1 0 0 1 0 1 1]
Example 17 — seed()
To reproduce the same random values:
Example 18 — Find Maximum and Minimum
Example 19 — Mean
The result should be approximately 0.5. Because values are uniformly distributed between 0 and 1.
Example 20 — Generate Random Matrix
Comparisons
random() vs rand()These two are very similar. Both produce random floats between 0 and 1. The main difference is syntax.
rand() - separate arguments:
random() - tuple argument:
random() vs randint()random() → Floating-point values
randint() → Integer values
Quick Cheat Sheet
Important distinction
rand()→ random floats, shape as separate argumentsrandn()→ normal-distribution valuesrandint()→ random integersrandom()→ random floats, shape usually as tuple
6. np.random.choice()
np.random.choice() is used to randomly select values from an array or sequence.
Syntax
| Parameter | Meaning |
|---|---|
a |
Values or integer range to choose from |
size |
Number/shape of values to select |
replace |
Whether the same value can be selected again |
p |
Probability of selecting each value |
Example 1 — Select one value
Possible output: 30
Example 2 — Select one value from a list
You don't have to create a NumPy array first.
Example 3 — Select multiple values
Possible output: [40 10 30]. Here, 3 means select 3 values.
Example 4 — Duplicate values are possible
By default: replace=True
Possible output: [20 10 20 30 10 10 30 20 30 10]. Notice 10, 20, and 30 are repeated.
Example 5 — replace=False
If you don't want duplicates:
Possible output: [40 10 30]. Every selected value is unique.
replace=False means once a value is selected, it cannot be selected again.
Example 6 — Select all values randomly
This is basically a random shuffle of the elements.
Practical Examples
Example 7 — Random Student
Possible output: Priya. Very useful classroom example: Randomly select one student for an activity.
Example 8 — Random Student Group
Example 9 — Coin Toss
Example 10 — Multiple Coin Tosses
Example 11 — Dice
Multiple rolls:
Using Probabilities (p Parameter)
Example 12 — p Parameter
p allows us to specify the probability of each value.
Suppose: A → 70%, B → 20%, C → 10%
The probabilities must add up to 1: 0.7 + 0.2 + 0.1 = 1
Example 13 — Multiple selections with probability
Because A has the highest probability, it will tend to occur more often.
Example 14 — Weighted Student Selection
Suppose we want to select students with different probabilities:
More Practical Scenarios
Example 15 — Random Color
Example 16 — Random Day
Example 17 — Random Product
Example 18 — 2-D Output
size can be a tuple.
Duplicates are possible because default is replace=True.
Example 19 — 3-D Output
Example 20 — Random Lottery Numbers
Suppose lottery numbers are from 1–50 and we want 6 unique numbers:
Example 21 — Random Exam Question
Example 22 — Random Team Selection
Example 23 — a as an Integer
An interesting shortcut:
This chooses from 0, 1, 2, 3, 4. It is equivalent to choosing from np.arange(5).
Example 24 — choice() with replace=False Error
Example 25 — Seed
Most Important Examples
Final Comparison of All 5
| Function | Main purpose | Example |
|---|---|---|
rand() |
Random float 0–1 | np.random.rand(3,3) |
randn() |
Normal distribution | np.random.randn(3,3) |
randint() |
Random integers | np.random.randint(1,100,5) |
random() |
Random float 0–1 | np.random.random((3,3)) |
choice() |
Select from given values | np.random.choice([10,20,30]) |
Important Modern NumPy Note
For new code, NumPy recommends the newer np.random.default_rng() API rather than relying on the older global np.random.* functions. However, understanding the global functions remains essential for reading and maintaining existing codebases.