# Numpy random range – Python NumPy random.ranf() Function

Numpy random range: 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 ranf() function of the NumPy random module is used to generate random float numbers in the half-open interval [0.0, 1.0).

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

Syntax:

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

Example1

Approach:

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

Example2

Approach:

• Import numpy module using the import keyword.
• Get random numbers using the numpy.random.ranf() 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.ranf() function in a given
# shape(row_size*col_size) and store it in a variable.
randm_num = np.random.ranf((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.76381366 0.23319446 0.60073586 0.60912952 0.23115309]
[0.90087426 0.44052239 0.92513066 0.38476573 0.91529723]
[0.09729554 0.78762806 0.6567433 0.21141971 0.39465675]
[0.12185161 0.45755803 0.80775674 0.13174433 0.3959227 ]]

Example3

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

(b-a) * np.random.ranf() + 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.ranf() 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.ranf() method ([15,30) range)
# Store it in a variable
randm_num = (30-15) * np.random.ranf((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 =
[[16.49140027 17.20487232 25.00107546 21.69392016 17.72331802]
[21.20329787 16.68319085 16.6773392 24.56214766 18.58925051]
[17.48327867 17.22259556 15.90261781 21.60549666 26.49168171]
[23.46352031 20.87223868 20.30891234 23.47354995 17.08046668]]