Numpy random.random() Function



The Numpy random.random() function is used to generate random numbers in the interval of [0.0, 1.0). It returns an array of specified shape and fills it with random floats in the half-open interval.

Using numpy.random.random() 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, 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.random() Function −

numpy.random.random(size=None)

Parameters

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

  • size (optional): This defines the output shape. If None, a single float is returned. If an integer, a 1D array of that length is returned. If a tuple, it specifies the dimensions of the output array.

Return Values

This function returns a NumPy array with random floats between 0 and 1.

Example

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

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

Output

Following is the output of the above code −

Random Value - 0.5488135039273248

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

The numpy.random.random() function used to generate the numpy array of random float values between 0 & 1.

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

import numpy as np
numpy_Array = np.random.random(size = 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.random() function, we can generate a 2-dimensional array of random values by specifying a tuple as the size parameter. This tuple represents the desired shape of the array.

In the following example, we have created a 2D array with 3 rows and 4 columns, filled with random values between 0 and 1 −

import numpy as np
# Generating a 2D array with random values between 0 and 1
random_2d_array = np.random.random((3, 4))
print("Numpy 2D Array of Random Values -\n", random_2d_array)

Output

Following is the output of the above code −

Numpy 2D Array of Random Values -
[[0.37454012 0.95071431 0.73199394 0.59865848]
 [0.15601864 0.15599452 0.05808361 0.86617615]
 [0.60111501 0.70807258 0.02058449 0.96990985]]
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