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
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.

