# Python NumPy random.random() Function

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() Function:

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

The random.random() 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.random() + a

Syntax:

numpy.random.random(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.random() Function in Python

Example1

Approach:

• Import numpy module using the import keyword.
• Get a single random float number using the numpy.random.random() 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.random() function
# and store it in a variable.
randm_num = np.random.random()
# Print the above generated random float 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.1331603208268296
<class 'float'>

Example2

Approach:

• Import numpy module using the import keyword.
• Get random numbers using the numpy.random.random() 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() function in a given
# shape(row_size*col_size) and store it in a variable.
randm_num = np.random.random((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.53425981 0.15464933 0.19704539 0.77025802 0.19868733]
[0.81420644 0.29648754 0.86901474 0.9175778 0.45379205]
[0.38433117 0.34967599 0.8883783 0.35546383 0.58073841]
[0.79020245 0.25522411 0.30891562 0.67491708 0.37420358]]

Example3

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

(b-a) * np.random.random() + 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.random() 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.random() method ([15,30) range)
# Store it in a variable
randm_num = (30-15) * np.random.random((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 =
[[17.8814533 25.75268862 17.85986386 25.41080662 15.34851729]
[22.14816824 21.45246145 24.34832825 17.50913458 24.67041805]
[20.02080082 27.07500204 24.4981532 15.25144775 29.85972124]
[19.16948171 28.5455416 19.54591574 17.62868895 18.24347181]]