AppDividend
Latest Code Tutorials

# np.random.rand: How to Create An Array with Random Values

The numpy.random.rand() method creates an array of specified shapes and fills it with random values.

## np.random.rand

The np.random.rand is a mathematical function used to create a ndarray with random values. The rand() function takes dimension, which indicates the dimension of the ndarray with random values. The np.random.rand(d0, d1, …, dn) method creates an array of specified shapes and fills it with random values.

### Syntax

```numpy.random.rand(dimension)
```

### Parameters

The np random rand() function takes one argument, which indicates the dimension of the ndarray with random values.

### Return Value

The rand() function returns an nd-array with a given dimension filled with random values.

## Programming Example

### Creating a 1D array using numpy.random.rand()

```# importing numpy
import numpy as np

# Now creating an 1D array of size 10
arr = np.random.rand(10)

print("Values of 1D array is:\n", arr)
print("Shape of the array is : ", np.shape(arr))
# Creating of size 5
arr2 = np.random.rand(5)
print("Values of the array is:\n ", arr2)
print("Shape of the array is : ", np.shape(arr2))
```

#### Output

```Values of 1D array is:
[0.17352283 0.16893341 0.76386692 0.03138259 0.59792538 0.76615978
0.84131927 0.61328718 0.32773058 0.65744193]
Shape of the array is :  (10,)
Values of the array is:
[0.90645146 0.83536319 0.29311148 0.2786733  0.87616911]
Shape of the array is :  (5,)
```

#### Explanation

In this example, we have imported numpy, and then we have first created an array of size 10, then we have printed it. After that, we have printed one array of size 5 using random.rand().

### Creating 2D array using numpy.random.rand()

```#importing numpy
import numpy as np

#Now creating an 2D array of size 4x5
arr=np.random.rand(4,5)

print("Values of 2D array is:\n",arr)
print("Shape of the array is : ",np.shape(arr))
#Creating of size 5x5
arr2=np.random.rand(5,5)
print("Values of the array is:\n ",arr2)
print("Shape of the array is : ",np.shape(arr2) )```

#### Output

```Values of 2D array is:
[[0.28861984 0.18593293 0.45034183 0.3699019  0.89226622]
[0.98189518 0.85439992 0.06126254 0.13031828 0.21441592]
[0.93145627 0.7457769  0.34015265 0.91147038 0.09517888]
[0.20201291 0.05616537 0.23150696 0.17963964 0.15687614]]
Shape of the array is :  (4, 5)
Values of the array is:
[[0.39721576 0.7976625  0.93283862 0.42401951 0.84877493]
[0.23814044 0.08125397 0.0431911  0.75650221 0.79033326]
[0.59390514 0.0592595  0.72867553 0.12893031 0.67127664]
[0.83742068 0.6945678  0.8227099  0.97034151 0.54721918]
[0.77069907 0.44371671 0.76421675 0.58487264 0.79122344]]
Shape of the array is :  (5, 5)```

#### Explanation

In this example, we have imported numpy, and then we have first created an array of size 4×5, then we have printed it. After that, we have printed one array of size 5×5 using random.rand().

### Constructing 3D array using numpy.random.rand()

```# importing numpy
import numpy as np

# Constructing an 3D array of size 2x2x2
arr = np.random.rand(2, 2, 2)

print("Values of 3D array is:\n", arr)
print("Shape of the array is : ", np.shape(arr))

# Constructing an array of size 1x2x3
arr2 = np.random.rand(1, 2, 3)
print("Values of 3D the array is:\n ", arr2)
print("Shape of the array is : ", np.shape(arr2))
```

#### Output

```Values of 3D array is:
[[[0.97177141 0.52129858]
[0.25837627 0.38226151]]

[[0.78923058 0.74976018]
[0.08221875 0.29096634]]]
Shape of the array is :  (2, 2, 2)
Values of 3D the array is:
[[[0.16431053 0.81809305 0.94068965]
[0.40489116 0.23819052 0.89154978]]]
Shape of the array is :  (1, 2, 3)```

#### Explanation

In this example, we have imported numpy, and then we have first created an array of size 2x2x2, then we have printed it. After that, we have printed one array of sizes 1x2x3 using np random.rand() function.

That’s it for this tutorial.