NP.random.rand() – Python NumPy random.rand() Function

NP.random.rand(): Random values in a specific shape are returned by the NumPy random.rand() function. The method generates an array of the specified shape and populates it with random samples obtained from a continuous uniform distribution over the range [0, 1).

The following relationship can be used to generate random values from unif[a, b), b>a:

(b-a) * np.random.rand() + a

Syntax:

numpy.random.rand(d0, d1, d2, ..., dn)

Parameters

Random.rand(): d0, d1, d2, …, dn: This is Optional. Specify the dimensions of the returned array, which must be entirely positive. If no arguments are provided, a single Python float is returned.

Return Value:

It Returns a set of random values in the shape specified.

NumPy random.rand() Function in Python

Example1

Approach:

  • Import numpy module using the import keyword.
  • Get a random number using the numpy.random.rand() function and store it in a variable.
  • Print the above generated random number.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Get a random number using the numpy.random.rand() function 
# and store it in a variable.
randm_num = np.random.rand()
# print the above generated random number
print("The random number generated = ", randm_num)

Output:

The random number generated = 0.14114364047169525

Example2

Approach:

  • Import numpy module using the import keyword.
  • Get random numbers using the numpy.random.rand() function in a given shape(row_size*col_size) and store it in a variable.
  • Print the above generated random numbers in a given shape.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Get random numbers using the numpy.random.rand() function in a given 
# shape(row_size*col_size) and store it in a variable.
randm_num = np.random.rand(4, 5)
# Print the above generated random numbers in a given shape.
print("The random numbers generated in a given shape =\n ", randm_num)

Output:

The random numbers generated in a given shape =
[[0.84399159 0.96751163 0.75330974 0.98124625 0.50085139]
[0.93878372 0.80936363 0.46275984 0.34299838 0.9416278 ]
[0.51676061 0.34870336 0.14311732 0.95984252 0.78771714]
[0.63220278 0.65999758 0.95409713 0.78054772 0.36499181]]

Example3

Random.rand(): We can define the uniform distribution to draw the sample from by using (b-a) * np.random.rand() + a relationship

Approach:

  • Import numpy module using the import keyword.
  • Applying the above formula by giving some random shape, b, and a values to generate random numbers.
  • Store it in a variable
  • Print the above generated random numbers using the formula given.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Applying the above formula by giving some random shape, b and a values
# to generate random numbers
# Store it in a variable
randm_num = (30-15) * np.random.rand(4, 5) + 15
# Print the above generated random numbers using the formula given.
print("The random numbers generated using the formula given =\n ", randm_num)

Output:

The random numbers generated using the formula given =
[[25.6565808 16.71057355 20.78746727 21.03278992 24.95020658]
[28.8047786  18.06276967 15.06002763  22.27933719 18.3527819 ]
[22.26829286 20.81859816 16.32016847 26.67076438 19.8136504 ]
[21.55532992 26.02711819 24.86898579 20.36485819 27.74491685]]