Np.random.random_sample – Python NumPy random.random_sample() Function

Np.random.random_sample: The random module is part of the NumPy library. This module includes the functions for generating random numbers. This module includes some basic random data generating methods, as well as permutation and distribution functions and random generator functions.

NumPy random.random_sample() Function:

Python numpy random float: The random_sample() function of Numpy random module is used to generate random float numbers in the half-open interval [0.0, 1.0).

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

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

Syntax:

numpy.random.random_sample(size=None)

Parameters

size: This is Optional. It specifies the output shape. If the supplied shape is (a, b, c), for example, a * b * c samples are drawn. The default value is None, which results in a single item being returned.

Return Value:

It Returns random values in a given shape (until size=None, which returns a single float).

NumPy random.random_sample() Function in Python

Example1

Approach:

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

Below is the implementation:

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

Output:

The random float number generated = 0.22280997367859634

Example2

Approach:

  • Import numpy module using the import keyword.
  • Get random numbers using the numpy.random.random_sample() 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.random_sample() function in a given 
# shape(row_size*col_size) and store it in a variable.
randm_num = np.random.random_sample((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.94769719 0.11868107 0.17124853 0.65756138 0.99969386]
 [0.68872441 0.2260948 0.88930798 0.6742404 0.40847078]
 [0.18087508 0.05167878 0.12525467 0.00150416 0.56213672]
 [0.85694563 0.20327345 0.22300069 0.08446226 0.05443388]]

Example3

Approach:

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

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np  
# Get a random float number using the numpy.random.random_sample() function 
# and store it in a variable.
randm_num = np.random.random_sample()
# Print the type of the above generated random number using the type function
print(type(randm_num)) 

Output:

<class 'float'>

Example4

We may define the uniform distribution to draw the sample from by using

(b-a) * np.random.random sample() + 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 using the random.random_sample() method.
  • 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 using the random.random_sample() method
# Store it in a variable
randm_num = (30-15) * np.random.random_sample((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 =
[[20.00062152 19.98246388 27.5794208 24.60204415 21.23107243]
 [18.00233343 25.64157724 26.31357584 20.63412897 16.84214466]
 [17.58225974 27.53799833 19.90766692 28.83894201 21.0092688 ]
 [27.61648283 22.8137593 19.72896524 15.85965 18.52678232]]