numpy.random.rand() in Python
Last Updated :
08 Mar, 2024
This article provides an in-depth exploration of the `numpy.random.rand()` function in Python. It covers the function's syntax, and definition, and includes illustrative examples with detailed explanations for better understanding.
numpy.random.rand() Function Syntax
The numpy.random.rand() function creates an array of specified shapes fills it with random values and generates random numbers with Numpy.
Syntax : numpy.random.rand(d0, d1, ..., dn)
Parameters:
- d0, d1, ..., dn : [int, optional]Dimension of the returned array we require, If no argument is given a single Python float is returned.
Return:
Array of defined shape, filled with random values.
What is numpy.random.rand() in Python ?
`numpy.random.rand()` in Python is a function from the NumPy library that generates an array of specified shapes and fills it with random values uniformly distributed between 0 and 1. It is commonly used for creating random arrays in various applications such as simulations and machine learning. The function's output is determined by the shape parameters provided.
Python numpy.random.rand() Examples
There are where use cases of numpy.random.rand() for Generating random numbers with NumPy. here we are explaining some advantages of numpy.random.rand() for Generating random numbers with Numpy those are following.
- Randomly Constructing 1D Array
- Randomly Constructing 2D Array
- Randomly Constructing 3D Array
Randomly Constructing 1D Array
In this example The code uses NumPy to generate a 1D array with 5 random values between 0 and 1 using the `numpy.random.rand()` method. The resulting array is printed to the console.
Python
# Python Program illustrating
# numpy.random.rand() method
import numpy as geek
# 1D Array
array = geek.random.rand(5)
print("1D Array filled with random values :", array);
Output :
1D Array filled with random values :
[ 0.84503968 0.61570994 0.7619945 0.34994803 0.40113761]
Randomly Constructing 2D Array
In this example This Python code uses the NumPy library to create a 3x4 2D array filled with random values between 0 and 1 using the `numpy.random.rand()` method. The resulting array is then printed to the console.
Python
# Python Program illustrating
# numpy.random.rand() method
import numpy as geek
# 2D Array
array = geek.random.rand(3, 4)
print("\n\n2D Array filled with random values : ", array);
Output :
2D Array filled with random values :
[[ 0.94739375 0.5557614 0.69812121 0.86902435]
[ 0.94758176 0.22254413 0.21605843 0.44673235]
[ 0.61683839 0.40570269 0.34369248 0.46799524]]
Randomly Constructing 3D Array
In this example The code uses the NumPy library to generate a 3D array of shape (2, 2, 2) filled with random values between 0 and 1 using the `numpy.random.rand()` method. The resulting array is then printed.
Python
# Python Program illustrating
# numpy.random.rand() method
import numpy as geek
# 3D Array
array = geek.random.rand(2, 2 ,2)
print("\n\n3D Array filled with random values : \n", array);
Output :
3D Array filled with random values :
[[[ 0.97942627 0.01068711]
[ 0.35749073 0.22484643]]
[[ 0.99733022 0.8029555 ]
[ 0.44111692 0.90537128]]]
Note : These codes won't run on online IDE's. So please, run them on your systems to explore the working.
Similar Reads
numpy.random.randn() in Python The numpy.random.randn() function creates an array of specified shape and fills it with random values as per standard normal distribution. If positive arguments are provided, randn generates an array of shape (d0, d1, ..., dn), filled with random floats sampled from a univariate ânormalâ (Gaussian)
3 min read
numpy.random.f() in Python With the help of numpy.random.f() method, we can get the random samples of F distribution and return the random samples of numpy array by using this method. Syntax : numpy.random.f(dfnum, dfden, size=None) Return : Return the random samples as numpy array. Example #1 : In this example we can see tha
1 min read
rand vs normal in Numpy.random in Python In this article, we will look into the principal difference between the Numpy.random.rand() method and the Numpy.random.normal() method in detail. About random: For random we are taking .rand() numpy.random.rand(d0, d1, ..., dn) : creates an array of specified shape and fills it with random values.
3 min read
numpy.random.zipf() in Python With the help of numpy.random.zipf() method, we can get the random samples from zipf distribution and return the random samples as numpy array by using this method. Zipf distribution Syntax : numpy.random.zipf(a, size=None) Return : Return the random samples as numpy array. Example #1 : In this exam
1 min read
numpy.random.power() in Python With the help of numpy.random.power() method, we can get the random samples from power distribution and return the random samples by using this method. power distribution Syntax : numpy.random.power(a, size=None) Return : Return the random samples as numpy array. Example #1 : In this example we can
1 min read