How to use np.random.random() in Python
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().
The np.random.random() function
The 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).
Syntax
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 withd1elements.size = (d1, d2): Generates a 2-D array of random numbers withd1*d2elements.size = (d1, d2, ..., dn): Generates an n-D array of random numbers withd1*d2*...*dnelements.
Coding examples
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.
Single random number
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)
Code explanation
Line 1: We import the
numpymodule, 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.
1-D array of random numbers
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)
Code explanation
Line 3: We pass in
size=4as the parameter that generates an array of random numbers with 4 elements in it.
2-D array of random numbers
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)
Code explanation
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.
Conclusion
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