Numpy random float – Python NumPy random.sample() Function

Numpy random float: 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.sample() Function:

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

The random.sample() function in NumPy returns random numbers in a specified shape. The method generates an array of the specified shape and populates it with random samples obtained from a continuous uniform distribution throughout the range [0.0, 1.0).

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

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

Syntax:

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

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

NumPy random.sample() Function in Python

Example1

Approach:

  • Import numpy module using the import keyword.
  • Get a single random float number using the numpy.random.sample() function and store it in a variable.
  • Print the above generated random float number.
  • 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 single random float number using the numpy.random.sample() function 
# and store it in a variable.
randm_num = np.random.sample()
# Print the above generated random number
print("The random float number generated = ", randm_num)
# Print the type of the above generated random number using the type function
print(type(randm_num)) 
 

Output:

The random float number generated = 0.8568248580543162
<class 'float'>

Example2

Approach:

  • Import numpy module using the import keyword.
  • Get random numbers using the numpy.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.sample() function in a given 
# shape(row_size*col_size) and store it in a variable.
randm_num = np.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.75833275 0.85300887 0.82333196 0.68069573 0.19687092]
 [0.07796941 0.68366033 0.35084489 0.47903854 0.88562277]
 [0.00829657 0.93846906 0.18563032 0.53647775 0.88976646]
 [0.38018368 0.1569671 0.32027987 0.46557875 0.99161896]]

Example3

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

(b-a) * np.random.sample() + a relationship. ([a, b) range)

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.sample() method ([15,30) range).
  • 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.sample() method ([15,30) range)
# Store it in a variable
randm_num = (30-15) * np.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 =
[[19.61266756 29.36582927 21.65632864 29.98500023 25.5652121 ]
 [20.31043748 21.52262871 21.96209214 27.25704154 22.36774245]
 [22.43531016 27.87364354 27.59003333 17.11866125 20.49256493]
 [19.81349368 29.0872826 17.66445181 25.25047414 20.53383984]]