Introduction
The NumPy Random module is used to generate random numbers, random arrays, and random selections. It is widely used in data science, machine learning, simulations, testing, and statistical analysis. In this chapter, you’ll practice beginner-friendly NumPy Random module questions with complete solutions. NumPy Random Module practice questions with solutions help to understand the concepts.
1. Python Program to Generate a Random Integer
Problem Statement
Write a Python program to generate a random integer between 1 and 100.
Python Solution
import numpy as np
number = np.random.randint(1, 101)
print(number)
Sample Output
57
Explanation
The randint() function generates a random integer within the specified range.
Concepts Covered
randint()- Random Integer
2. Python Program to Generate an Array of Random Integers
Problem Statement
Write a Python program to generate an array of five random integers.
Python Solution
import numpy as np
numbers = np.random.randint(1, 50, size=5)
print(numbers)
Sample Output
[12 35 7 41 28]
Explanation
The size parameter specifies how many random values should be generated.
Concepts Covered
- Random Array
size
3. Python Program to Generate Random Floating-Point Numbers
Problem Statement
Write a Python program to generate five random decimal numbers.
Python Solution
import numpy as np
numbers = np.random.rand(5)
print(numbers)
Sample Output
[0.45 0.87 0.12 0.66 0.93]
Explanation
The rand() function generates random floating-point numbers between 0 and 1.
Concepts Covered
rand()- Random Float
4. Python Program to Generate a 2D Random Array
Problem Statement
Write a Python program to generate a 3 × 3 random integer array.
Python Solution
import numpy as np
numbers = np.random.randint(1, 10, size=(3, 3))
print(numbers)
Sample Output
[[4 8 1]
[7 5 2]
[9 6 3]]
Explanation
The size parameter accepts a tuple to create multi-dimensional arrays.
Concepts Covered
- 2D Random Array
randint()
5. Python Program to Select a Random Element
Problem Statement
Write a Python program to randomly select one element from a NumPy array.
Python Solution
import numpy as np
numbers = np.array([10, 20, 30, 40, 50])
result = np.random.choice(numbers)
print(result)
Sample Output
30
Explanation
The choice() function randomly selects one element from an array.
Concepts Covered
choice()- Random Selection
6. Python Program to Shuffle a NumPy Array
Problem Statement
Write a Python program to shuffle the elements of a NumPy array.
Python Solution
import numpy as np
numbers = np.array([1, 2, 3, 4, 5])
np.random.shuffle(numbers)
print(numbers)
Sample Output
[3 5 1 2 4]
Explanation
The shuffle() function changes the order of elements in the original array.
Concepts Covered
shuffle()- Random Order
7. Python Program to Generate a Random Permutation
Problem Statement
Write a Python program to create a random permutation of an array.
Python Solution
import numpy as np
numbers = np.array([1, 2, 3, 4, 5])
result = np.random.permutation(numbers)
print(result)
Sample Output
[4 1 5 2 3]
Explanation
The permutation() function returns a shuffled copy without modifying the original array.
Concepts Covered
permutation()- Random Permutation
8. Python Program to Set a Random Seed
Problem Statement
Write a Python program to generate repeatable random numbers using seed().
Python Solution
import numpy as np
np.random.seed(10)
print(np.random.randint(1, 100))
Sample Output
10
Explanation
The seed() function ensures that the same random values are generated every time the program runs.
Concepts Covered
seed()- Reproducible Results
9. Python Program to Generate Random Boolean Values
Problem Statement
Write a Python program to randomly generate Boolean values.
Python Solution
import numpy as np
values = np.random.choice([True, False], size=5)
print(values)
Sample Output
[ True False False True False]
Explanation
The choice() function can also randomly select Boolean values.
Concepts Covered
- Boolean Values
- Random Choice
10. Python Program to Generate a Random Decimal Between 5 and 10
Problem Statement
Write a Python program to generate a random decimal number between 5 and 10.
Python Solution
import numpy as np
number = np.random.uniform(5, 10)
print(number)
Sample Output
7.63
Explanation
The uniform() function generates a random floating-point number within the specified range.
Concepts Covered
uniform()- Random Decimal
11. Python Program to Generate Random Integers Using NumPy
Problem Statement
Write a Python program to generate a 4×5 NumPy array containing random integers between 10 and 99.
Python Solution
import numpy as np
# Generate a 4×5 array of random integers
random_array = np.random.randint(10, 100, size=(4, 5))
print("Random Integer Array:")
print(random_array)
Sample Output
Random Integer Array:
[[34 91 18 67 42]
[85 29 73 56 11]
[98 40 65 24 77]
[19 88 53 36 70]]
Note: Your output will be different each time because random values are generated.
Explanation
np.random.randint()generates random integers within a specified range.size=(4,5)creates a matrix with 4 rows and 5 columns.
Concepts Covered
- NumPy Random Module
- randint()
- Random Integer Generation
12. Python Program to Generate Random Floating-Point Numbers
Problem Statement
Write a Python program to generate 10 random floating-point numbers between 0 and 1.
Python Solution
import numpy as np
random_numbers = np.random.rand(10)
print("Random Floating-Point Numbers:")
print(random_numbers)
Sample Output
Random Floating-Point Numbers:
[0.26 0.87 0.45 0.13 0.99 0.54 0.72 0.38 0.66 0.14]
Explanation
The np.random.rand() function generates random decimal numbers uniformly distributed between 0 and 1.
Concepts Covered
- rand()
- Floating-Point Numbers
- Uniform Distribution
13. Python Program to Generate Random Numbers from a Normal Distribution
Problem Statement
Write a Python program to generate 20 random numbers following a normal distribution with:
- Mean = 50
- Standard Deviation = 10
Python Solution
import numpy as np
random_numbers = np.random.normal(loc=50, scale=10, size=20)
print("Random Numbers from Normal Distribution:")
print(random_numbers)
Sample Output
Random Numbers from Normal Distribution:
[42.3 57.8 51.4 61.2 39.5 47.8 55.1 63.9 ...]
Explanation
locspecifies the mean.scalespecifies the standard deviation.sizespecifies how many random values are generated.
Concepts Covered
- normal()
- Gaussian Distribution
- Mean
- Standard Deviation
14. Python Program to Shuffle Elements of a NumPy Array
Problem Statement
Write a Python program to shuffle the elements of a NumPy array randomly.
Python Solution
import numpy as np
array = np.arange(1, 11)
print("Original Array:")
print(array)
np.random.shuffle(array)
print("\nShuffled Array:")
print(array)
Sample Output
Original Array:
[1 2 3 4 5 6 7 8 9 10]
Shuffled Array:
[5 2 9 7 1 10 3 6 4 8]
Explanation
The shuffle() function rearranges the elements of the original array in random order.
Concepts Covered
- shuffle()
- Random Rearrangement
- In-place Modification
15. Python Program to Select Random Elements Without Repetition
Problem Statement
Write a Python program to randomly select 5 unique numbers from an array without repeating any value.
Python Solution
import numpy as np
array = np.arange(1, 21)
selected = np.random.choice(array, size=5, replace=False)
print("Original Array:")
print(array)
print("\nRandomly Selected Elements:")
print(selected)
Sample Output
Original Array:
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]
Randomly Selected Elements:
[18 6 13 2 10]
Explanation
choice()randomly selects values.replace=Falseensures no value is selected more than once.
Concepts Covered
- choice()
- Random Sampling
- Unique Selection
16. Python Program to Generate a Random Matrix and Find Its Maximum and Minimum Values
Problem Statement
Write a Python program to generate a 5×5 random matrix and display its maximum and minimum values.
Python Solution
import numpy as np
matrix = np.random.randint(1, 101, size=(5, 5))
print("Random Matrix:")
print(matrix)
print("\nMaximum Value:", np.max(matrix))
print("Minimum Value:", np.min(matrix))
Sample Output
Random Matrix:
[[56 11 93 28 74]
[35 69 44 81 17]
[62 99 51 23 76]
[15 84 38 97 40]
[53 20 71 60 88]]
Maximum Value: 99
Minimum Value: 11
Explanation
The program creates a random matrix and uses np.max() and np.min() to identify the largest and smallest values.
Concepts Covered
- Random Matrix
- max()
- min()
17. Python Program to Generate Random Numbers Using a Fixed Seed
Problem Statement
Write a Python program to generate random numbers using a fixed seed so that the output remains the same every time.
Python Solution
import numpy as np
np.random.seed(42)
random_numbers = np.random.randint(1, 100, size=10)
print("Random Numbers:")
print(random_numbers)
Sample Output
Random Numbers:
[52 93 15 72 61 21 83 87 75 75]
Explanation
The seed() function initializes the random number generator with a fixed value, making the sequence reproducible. This is especially useful for testing, debugging, and machine learning experiments.
Concepts Covered
- seed()
- Reproducible Results
- Random Number Generator
18. Python Program to Simulate Rolling Two Dice Using NumPy
Problem Statement
Write a Python program to simulate rolling two six-sided dice 10 times using NumPy. Display the result of each roll and calculate the total for each pair.
Python Solution
import numpy as np
# Simulate rolling two dice 10 times
dice_rolls = np.random.randint(1, 7, size=(10, 2))
print("Dice Rolls:")
print(dice_rolls)
print("\nSum of Each Roll:")
print(np.sum(dice_rolls, axis=1))
Sample Output
Dice Rolls:
[[3 5]
[6 2]
[1 4]
[5 5]
[2 6]
[4 3]
[6 1]
[2 2]
[5 1]
[3 6]]
Sum of Each Roll:
[ 8 8 5 10 8 7 7 4 6 9]
Explanation
randint(1, 7)generates numbers from 1 to 6, representing dice faces.- Each row represents one roll of two dice.
np.sum(..., axis=1)calculates the total of each pair of dice.
Concepts Covered
- Random Simulation
- randint()
- sum()
- axis Parameter
- Real-World Applications
Chapter Summary
In this chapter, you learned how to generate random integers, floating-point numbers, random arrays, random selections, shuffled arrays, permutations, and reproducible random values using the NumPy Random module. These techniques are commonly used in machine learning, simulations, data analysis, and software testing.
Key Takeaways
randint()generates random integers.rand()generates random floating-point numbers.choice()randomly selects elements from an array.shuffle()changes the original array order.permutation()returns a shuffled copy.seed()generates reproducible random values.uniform()generates random decimal numbers within a range.
Frequently Asked Questions (FAQs)
1. What is the NumPy Random module?
The NumPy Random module provides functions to generate random numbers and random arrays for data analysis, testing, and machine learning.
2. What is the difference between shuffle() and permutation()?
shuffle() modifies the original array, while permutation() returns a shuffled copy and leaves the original array unchanged.
3. Why is seed() important?
seed() helps generate the same sequence of random numbers every time the program runs, making results reproducible.
4. Which function generates random integers?
Use np.random.randint() to generate random integers.
5. How do I generate random decimal numbers?
Use np.random.rand() for values between 0 and 1, or np.random.uniform() for a custom range.
6. Can I randomly select values from an array?
Yes. Use np.random.choice() to randomly select one or more elements from an array.
7. Where is the NumPy Random module used?
It is widely used in data science, machine learning, simulations, game development, statistical analysis, and software testing.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

