Python:NumPy .rand()
The .rand() function from the NumPy library returns an array of the provided shape filled with randomly generated samples from a uniform distribution over [0, 1). The shape of the output array is decided based on the arguments for the function.
This function is commonly used for creating random arrays in applications like machine learning and simulations.
Syntax
numpy.random.rand(d0, d1, ..., dn)
Parameters:
d0, d1, ..., dn(int, optional): Dimensions of the returned array. Each parameter represents the size along a particular axis.
Return value:
An array (ndarray) of shape (d0, d1, ..., dn) filled with random floats from a uniform distribution over [0, 1).
Example
This example demonstrates the use of .rand():
import numpy as np# Using .rand() to generate a single float valuesingle_float = np.random.rand()print("This is a single float:", single_float, "\n")# Using .rand() to generate a 1D array with 5 elementsoneD_array = np.random.rand(5)print("This is a 1D array with 5 elements:", oneD_array, "\n")# Using .rand() to generate a 2D array with shape (3, 4)twoD_array = np.random.rand(3, 4)print("This is a 2D array with shape (3, 4):", twoD_array, "\n")# Using .rand() to generate a 3D array with shape (2, 4, 3)fourD_array = np.random.rand(2, 4, 3)print("This is a 3D array with shape (2, 4, 3):", fourD_array, "\n")
The possible ouput for this code can be:
This is a single float: 0.062282333140694646This is a 1D array: [0.26135751 0.68792718 0.29413907 0.96249664 0.96284554]This is a 2D array with shape (3, 4): [[0.43097673 0.1741973 0.92504336 0.70587898][0.04921928 0.55717071 0.66223492 0.52897239][0.43088071 0.72260989 0.77430222 0.91549911]]This is a 4D array with shape (2, 4, 3): [[[0.66207525 0.72505789 0.56002624][0.49060286 0.68176017 0.5740969 ][0.60655012 0.83349518 0.85648538][0.18412521 0.29999511 0.89179013]][[0.29378119 0.08597027 0.68488609][0.95905169 0.24037647 0.21941708][0.17337005 0.86617885 0.45531762][0.06081413 0.08813921 0.34166149]]]
Note: The output of
np.random.rand()may change each time because it generates pseudo-random numbers based on the system’s internal state, which may vary with each execution.
Codebyte Example
This codebyte is an example of .rand() in use to generate different dimensional arrays:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours