# 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.

For more NumPy examples refer to NumPy Tutorial. 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.

## 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.

``````
# Below are the quick examples

# 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` – The dimension of the returned array and it must be int type. If no argument is specified a single Python float is returned.

### 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 `random.rand()` is a numpy library function that returns an array of random samples from the `uniform distribution over [0,1]`. It allows dimensions as an argument and returns an array of specified dimensions. If we 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 as np
# Use random.rand() function
arr = np.random.rand()
print(arr)

# Output
# 0.7507775570160498
``````

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

``````
# Use numpy.random.seed() function
np.random.seed(0)
arr = np.random.rand()
print(arr)

# Output
# 0.5488135039273248
``````

## 4. Get 1-D NumPy Array of Random Values

Pass the shape of the array as an argument into `random.rand()`function to create a one-dimensional NumPy array of random values. This function will return an array of a given dimension. The below example returns 6 random values with shape 1,6 (1 row and 6 columns).

``````
# Get 1-dimensional array of random values
arr = np.random.rand(6)
print(arr)

# Output
# [0.56804456 0.92559664 0.07103606 0.0871293  0.0202184  0.83261985]
``````

## 5. Get 2-D NumPy Array of Random Values

To get random values of two-dimensional arrays, pass (tuple of ints) the shape of the array with value 2 or more for rows. It returns the two-dimensional array of specified shape.

``````
# Get 2-Dimensional array of random values
arr = np.random.rand(2,5)
print(arr)

# Output
# [[0.77815675 0.87001215 0.97861834 0.79915856 0.46147936]
# [0.78052918 0.11827443 0.63992102 0.14335329 0.94466892]]
``````

## 6. Get 3-D NumPy Array of Random Values

Let’s generate a three-dimensional random array with a specified shape. Similarly, you can also generate any random arrays of any size using the `numpy.random.rand()` function.

``````
# Generate 3-dimensional array of random values
arr = np.random.rand(5,2,4)
print(arr)
``````

Yields below output.

``````
[[[0.52184832 0.41466194 0.26455561 0.77423369]
[0.45615033 0.56843395 0.0187898  0.6176355 ]]

[[0.61209572 0.616934   0.94374808 0.6818203 ]
[0.3595079  0.43703195 0.6976312  0.06022547]]

[[0.66676672 0.67063787 0.21038256 0.1289263 ]
[0.31542835 0.36371077 0.57019677 0.43860151]]

[[0.98837384 0.10204481 0.20887676 0.16130952]
[0.65310833 0.2532916  0.46631077 0.24442559]]

[[0.15896958 0.11037514 0.65632959 0.13818295]
[0.19658236 0.36872517 0.82099323 0.09710128]]]
``````

## 7. Conclusion

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

Happy Learning!!