NumPy Random Number Exercise

You will find practice questions related to the random number generation in NumPy on this page. Practicing these exercise questions will solve your doubts and improve your understanding of different ways of generating random numbers in NumPy.

In NumPy, there is a random module that has various functions such as randint(), rand(), uniform(), choice(), and others for generating random numbers. So, to solve these questions quickly, it is recommended that you know these functions. If you want to learn more about the random module and their functions, visit the following tutorial:

Q1 Create a 1 dimensional NumPy random array a and sort it.

Solution

import numpy as np
a = np.random.rand(5)
print(np.sort(a))

Output

[0.01170141 0.09909724 0.47288824 0.52449332 0.82644721]

Q2 Create a 5x5 NumPy array r that contains 25 uniformly distributed (pseudo) random numbers between 0 and 1.

Solution

import numpy as np
r = np.random.uniform(0, 1, (5, 5))
print(r)

Output

[[0.56804456 0.92559664 0.07103606 0.0871293  0.0202184 ]
 [0.83261985 0.77815675 0.87001215 0.97861834 0.79915856]
 [0.46147936 0.78052918 0.11827443 0.63992102 0.14335329]
 [0.94466892 0.52184832 0.41466194 0.26455561 0.77423369]
 [0.45615033 0.56843395 0.0187898  0.6176355  0.61209572]]

Your output may be different from the one shown above.

Q3 Create a 2x3 NumPy array that contains 6 normally distributed random numbers with mean 10 and standard deviation 5.

Solution

import numpy as np
arr = np.random.normal(10, 5, (2, 3))
print(arr)

Output

[[ 7.20087584  1.9835701   9.30366306]
 [-5.86759191  9.53430873 12.679931  ]]

Q4 Create a 2x3 NumPy array that contains 6 random integers between 2 and 6.

Solution

import numpy as np
arr = np.random.randint(2, 6, (2, 3))
print(arr)

Output

[[5 3 3]
 [5 5 4]]

To learn more about randint(), visit How to use NumPy random randint?

Q5 Use NumPy functions only, create an array of 10 random numbers in standard normal distribution.

Solution

import numpy as np
arr = np.random.normal(size=10)
print(arr)

Output

array([-0.04647527, -0.43817178,  0.37162942,  0.48765696,  0.4633344 ,
        0.95593161,  1.93810509, -2.24209764,  0.55227006,  0.79033699])

Q6 Create a NumPy array of 10 randomly generated values from 0 to 100.

Solution

randint() function is used to generate random integer values. As we want 10 random values so we will use size parameter to specify that.

import numpy as np
arr = np.random.randint(0, 100, size=10)
print(arr)

Output

[23 22 11 88 13 85 10  3 27 63]

Q7 Use NumPy random number function to generate at least 1000 numbers between the range of 1 to 100,000. After generating the array, performs the following operations:

  1. Sort all these numbers and store them into another NumPy array.
  2. Determine the maximum and minimum element.
  3. Determine the average of all the numbers stored in the NumPy array.

Solution

import numpy as np
arr = np.random.randint(1, 100000, size=1000)
print('NumPy array:\n', arr)
sorted_arr = np.sort(arr)
print('Sorted array:\n', sorted_arr)
print('Maximum element is ', sorted_arr[0])
print('Minimum element is ', sorted_arr[999])
print('Average is ', np.mean(arr))

Q8 Write a python code that creates a numpy array of 10 randomly generated values between 0 and 100.

Solution

import numpy as np
arr = np.random.randint(0, 100, size=10)
print('NumPy array:\n', arr)

Recommended Posts