• Post author:
  • Post category:NumPy / Python
  • Post last modified:March 27, 2024
  • Reading time:13 mins read
You are currently viewing How to Use Numpy random.rand() in Python

NumPy random.rand() function in Python is used to return random values from a uniform distribution in a specified shape. This function creates an array of the given shape and it fills with random samples from the uniform distribution. This function takes a tuple, to specify the size of an array, which behavior same as the other NumPy functions like the numpy.ones() function and numpy.zeros() function.

Advertisements

In this article, I will explain how to use the NumPy random.rand() function syntax and how it is used to create an array with random values based on a given shape with examples. For more NumPy examples refer to NumPy Tutorial.

1. Quick Examples of random.rand() Function

If you are in a hurry, below are some quick examples of how to use the Python NumPy random.rand() function.


# Quick examples of random.rand() function

# Example 1: Use numpy.random.rand() function
arr = np.random.rand()

# Example 2: Use numpy.random.seed() function
np.random.seed(0)
arr = np.random.rand()

# Example 3: Create 1-dimensional array 
# With random values
arr = np.random.rand(6)

# Example 4: Get 2-D Randomly array
arr = np.random.rand(2,5)

# Example 5: Generate 3-dimensional arrays 
# With random values
arr = np.random.rand(5,2,4)

2 Syntax of random.rand()

Following is the syntax of the numpy.random.rand() function.


# Syntax of NumPy random.rand()
random.rand(d0, d1, ..., dn) 

2.1 Parameters of random.rand()

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

  • d0, d1, …, dn – These are the dimensions of the output array. They specify the shape of the array you want to create.

2.2 Return Value

It returns a random array of specified shapes, filled with random values of float type from a uniform distribution over [0,1].

3. Usage of NumPy random.rand()

The numpy.random.rand() function generates random numbers from a uniform distribution over the range [0, 1). It returns an array of specified shapes and fills it with random values. It allows dimensions as an argument and returns an array of specified dimensions. If you don’t provide any argument, it will return the float value.

This function returns the random number without passing any parameter. You might get different random numbers when you run the same code multiple times. Let’s take an example,


# Import numpy 
import numpy as np

# Use random.rand() function
arr = np.random.rand()
print("Random value:\n",arr)

Yields below output.

numpy random rand

Alternatively, use the np.random.seed() function to avoid the above problem. It will return the same result with every execution by setting the seed() value.

You have used np.random.seed(0) to set the seed for the random number generator. This means that the sequence of random numbers generated after setting the seed will be the same every time you run the code.


# Use numpy.random.seed() function
np.random.seed(0)
arr = np.random.rand()
print("Random seed value:\n",arr)

# Output:
# Random seed value:
#  0.5488135039273248

4. Get 1-D NumPy Array of Random Values

To generate a 1-D NumPy array of random values, you can use the numpy.random.rand() function and specify the desired size of the array. For instance, np.random.rand(6) generates an array of 6 random numbers between 0 and 1. You can adjust the size of the array by changing the argument passed to np.random.rand() function.


# Get 1-dimensional array of random values
arr = np.random.rand(6)
print("After getting a 1D array of random values:\n",arr)

# Output:
# After getting a 1D array of random values:
#  [0.54019408 0.50597572 0.18926709 0.27325964 0.6813522  0.63123887]

5. Get 2-D NumPy Array of Random Values

To generate a 2-D NumPy array of random values, you can use the numpy.random.rand() function and specify the desired shape of the array.

In the below example, np.random.rand(2, 5) would generate a 2-D array with 2 rows and 5 columns filled with random numbers between 0 and 1. You can adjust the shape of the array by changing the arguments passed to np.random.rand().


# Get 2-Dimensional array of random values
arr = np.random.rand(2,5)
print("After getting a 2D array of random values:\n",arr)

# Output:
# After getting a 2D array of random values:
#  [[0.84064865 0.21361168 0.10496061 0.31468754 0.21481534]
#  [0.651011   0.61118366 0.51445752 0.90243079 0.78386559]]

6. Get 3-D NumPy Array of Random Values

To generate a 3-D NumPy array of random values, you can use the numpy.random.rand() function and specify the desired shape of the array.

In the below example, generating a 3-dimensional NumPy array of random values with the shape (5, 2, 4). The np.random.rand() function is used to create an array filled with random numbers from a uniform distribution between 0 and 1. In this case, you have generated a 3D array with 5 blocks, each block containing 2 rows and 4 columns of random values.


# Generate 3-dimensional array of random values
arr = np.random.rand(5,2,4)
print("After generating a 3D array of random values:\n",arr)

Yields below output.


# Output:
After generating a 3D array of random values:
 [[[0.6783926  0.09204763 0.75119182 0.71188557]
  [0.99563633 0.62722228 0.53880308 0.04984016]]

 [[0.77882176 0.71285523 0.49303025 0.52443372]
  [0.99048536 0.36337684 0.29299482 0.6720936 ]]

 [[0.80099436 0.99142718 0.3627374  0.28817001]
  [0.56565398 0.25528728 0.46530822 0.24171193]]

 [[0.63588997 0.32893566 0.50564156 0.90082209]
  [0.16500015 0.56542853 0.76169926 0.00541278]]

 [[0.60238266 0.35911697 0.37997381 0.94986615]
  [0.49177673 0.87886046 0.00530952 0.45702587]]]

Frequently Asked Questions

What does numpy.random.rand() do?

numpy.random.rand() is a function in the NumPy library used to generate random numbers. It returns an array of specified shape with random values uniformly distributed between 0 and 1.

How to generate a 1-D array of random values using numpy.random.rand()?

You can generate a 1-D array of random values using numpy.random.rand(), you simply need to specify the desired size of the array as an argument to the function.

Can I generate multi-dimensional arrays using numpy.random.rand()?

You can generate multi-dimensional arrays using numpy.random.rand() by specifying the desired shape of the array as arguments.

How to set a seed value for reproducibility?

You can set a seed value using numpy.random.seed(seed_value). This ensures that the random numbers generated are reproducible across runs.

Can I generate random integers using numpy.random.rand()?

numpy.random.rand() specifically generates random floating-point numbers between 0 and 1 from a uniform distribution. If you need random integers, you should use numpy.random.randint

Conclusion

In this article, I have explained the NumPy random.rand() function syntax, parameter, and usage of how to generate the random value of single and multi-dimensional arrays based on specified dimensions with examples.

Happy Learning!!

References