NumPy is a fundamental package that is used to perform numerical computation tasks in Python. NumPy is based on the ndarray (n-dimensional array) used to perform operations on large data sets at high speed. The reason for speedily performing operations is the pre-compiled and optimized C code which is working behind the NumPy operations.
Being a powerful library, NumPy provides the np.random
module which contains functions to generate random numbers. In this Answer, we will look into one of the functions of the np.random
module: np.random.random()
.
np.random.random()
functionThe np.random.random()
function is used to generate random numbers in the interval of [0.0, 1.0).
Note: When representing intervals, square bracket
[
means that the number is included in the interval, and round bracket)
means that the number is not included in the interval.
Using the function, the generated random numbers will be in a range greater than or equal to 0 (>=0) and less than 1 (<1).
The syntax to create random numbers using the .random()
function is:
np.random.random(size = (d1, d2, d3, ...))
The function takes in the size
parameter, which defines the shape of the generated random numbers.
size = ()
: Generates a single random number.
size = (d1)
: Generates a 1-D array of random numbers with d1
elements.
size = (d1, d2)
: Generates a 2-D array of random numbers with d1*d2
elements.
size = (d1, d2, ..., dn)
: Generates an n-D array of random numbers with d1*d2*...*dn
elements.
Now that we have explored the syntax of the np.random.random()
function, let's explore some examples for generating random numbers of different dimensions.
To generate a single random number, no parameters are passed in the .random()
function. Please click the "Run" button below to view the random number.
import numpy as nprandom_number = np.random.random()print(random_number)
Line 1: We import the numpy
module, which will provide us the .random()
function.
Line 3: We define the np.random.random()
function without any parameters, and it generates the random number.
To generate a 1-D array of random numbers, we have to pass the size of the array in the function. Please click the "Run" button below to generate a 1-D array of random numbers.
import numpy as nprandom_number = np.random.random(size = 4)print(random_number)
Line 3: We pass in size=4
as the parameter that generates an array of random numbers with 4 elements in it.
To generate a 2-D array of random numbers, we have to pass size=(rows, columns)
as the parameter. Please click the "Run" button below to generate a 2-D array of random numbers.
import numpy as nprandom_number = np.random.random(size = (4,3))print(random_number)
Line 3: We pass in the size=(4,3)
as the parameter that generates a 2-D array of random numbers with 4 rows and 3 elements in each row.
np.random.random()
makes it easier to generate random numbers that play a crucial role in machine learning tasks, where we have to initialize model parameters, shuffle data, and create random numbers for testing and validation.
Free Resources