Numpy random.rand() Function



The Numpy random.rand() function is used to generate a numpy array of specified shapes and fills with random values uniformly distributed between 0 and 1. This function is particularly used to generate random samples for simulations, testing, or initializing data.

Using numpy.random.rand() function, the generated random numbers will be in a range greater than or equal to 0 (>=0) and less than 1 (<1). When representing intervals - [0,1), square bracket [ means that the number is included in the interval, and round bracket ) means that the number is not included in the interval.

Syntax

Following is the syntax of the Numpy random.rand() function −

numpy.random.rand(d0, d1, ..., dn)

Parameters

Following are the parameters of the Numpy random.rand() function −

  • d0, d1, ..., dn - These are the dimensions of the output array.

Return Values

This function returns a NumPy array filled with random float values between 0 and 1.

Example

Following is a basic example to generate a evenly spaced numpy array using Numpy random.rand() function −

import numpy as np
random_value = np.random.rand()
print("Random Value -", random_value)
print(type(random_value))

Output

Following is the output of the above code −

Random Value - 0.5957341075780259
<class 'float'>

Example : Numpy Array using 'random.rand()'

The numpy.random.rand() function is used to generate a NumPy array of random float values between 0 and 1.

In the following example, we have generated a NumPy array using the numpy.random.rand() function −

import numpy as np
numpy_Array = np.random.rand(4)
print("Numpy 1D Array - \n", numpy_Array)
print(type(numpy_Array))

Output

Following is the output of the above code −

Numpy 1D Array - 
 [0.43857993 0.75618948 0.72346477 0.74740608]
<class 'numpy.ndarray'>

Example : Multi-dimensional Numpy Array

Using the numpy.random.rand() function, we can generate a n-dimensional array of random values by specifying the dimensions as parameters.

In the following example, we have created a 3D array with dimensions 3x4x3, filled with random values between 0 and 1 −

import numpy as np
numpy_3d_array = np.random.rand(3, 4,3)
print("Numpy 3D Array of Random Values -\n", numpy_3d_array)

Output

Following is the output of the above code −

Numpy 3D Array of Random Values -
 [[[0.04370878 0.40101641 0.87128492]
  [0.03316436 0.65729078 0.83986302]
  [0.74107696 0.56756153 0.02978992]
  [0.76785574 0.25137198 0.67751803]]

 [[0.63411997 0.20633224 0.27037647]
  [0.87758893 0.05098912 0.46860395]
  [0.20551389 0.18509449 0.65965151]
  [0.94767091 0.6158867  0.7130623 ]]

 [[0.32199802 0.75195402 0.72011531]
  [0.78364401 0.26592789 0.51386832]
  [0.950848   0.71341246 0.73289238]
  [0.71042609 0.05504133 0.00693914]]]
numpy_array_creation_routines.htm
Advertisements
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy