# How to create numpy array of random values

In this post, we are going to learn how to create a numpy array of random values by using the built-in function of the Python numpy library. We will understand how to create a numpy array of random integer range and float range for example by using functions like randint(),randn(), uniform(),rand().

### 1. randint() to create numpy random integer array

The numy randint() function is used to create a numpy array of a given size and fill it with random integer values from low to high. In this example, we are creating three different arrays.

• First we have created an array(npArr1) of range 5 mean 0 to 4,without explicitly specify size.
• Secondly,We have created an integer 2D array(npArr2) of range -9 to 9 of size(3,3)
• We have created a 3D(npArr3) array of range -9,9 of size(3,3,3) and of type=int64.
```import numpy as np

npArr1 = np.random.randint((3,3))
npArr2 = np.random.randint(-9,9,size=(3,3))
npArr3 = np.random.randint(-9,9,(3,3,3),dtype = 'int64')

print('Array1:',npArr1)
print('\nArray2:\n',npArr2)
print('\nArray3:\n',npArr3)

```

Output

```Array1: 2

Array2:
[[-9 -1  7]
[-3  7 -3]
[ 4  7 -3]]

Array3:
[[[ 6  5  8]
[ 3 -4 -3]
[-1 -1  3]]

[[-8 -3  8]
[-6 -8 -2]
[ 2 -7  7]]

[[ 2  3 -8]
[-4 -4 -8]
[ 7 -6 -5]]]
```

### 2. Randn() to create NumPy array of random float

The numpy.random.randn() creates an array of given size and filled array with random float point values.It always fills with floating point values based on normal distribution that mean=0 and variation =1.

• In python program we are generating an array of random float values.
• In First array,we have created single element array.
• In Secondly,We have created a 3D float randon values array.
```import numpy as np

RandArr1 = np.random.randn()
print('Array1 :\n ',RandArr1)

RandArr2 = np.random.randn(3,3)
print('Array2:\n',RandArr2)
```

Output

```Array1 :
2.105675973847657
Array2:
[[-2.06828682 -1.96888667  1.2966816 ]
[ 0.52071119  1.29561153 -0.16024068]
[-0.70473918 -0.48881628 -0.9445343 ]]
```

### 3. rand() to create numpy array of random number

The numpy rand() function create any array of give size and filled array with random float values.In this python program example we are creating numpy random array of float values. we have created a 1D array,2D,3D array of random float values .

```import numpy as np

floatArr1 = np.random.rand(3)

floatArr2 = np.random.rand(3,3)* 90

float3DArr = np.random.rand(3,3,3)

print('1D Array:',floatArr1 )

print('\n 2D array:\n',floatArr2)
print('\n 3D array:\n',float3DArr)

```

Output

```1D Array: [0.08462038 0.62049856 0.95784534]

2D array:
[[23.67329683 49.01425989 26.56671876]
[33.37048705 33.29069085  5.34752771]
[17.78822666 17.91621475 24.10043144]]

3D array:
[[[0.85613099 0.33067513 0.48199732]
[0.42555141 0.05531362 0.64478925]
[0.01190786 0.4152075  0.43616312]]

[[0.72474622 0.87224942 0.2261772 ]
[0.46862327 0.96057412 0.83153186]
[0.50698455 0.8776099  0.72899914]]

[[0.39892822 0.46384381 0.54913013]
[0.50658469 0.98334474 0.0085594 ]
[0.82913474 0.4306317  0.71063243]]]
```

### 4. Numy random_sample to create array random float

In this example, we are creating using the numpy random_sample() function to create a random numpy array of float values of size(3,3).

```import numpy as np

npArr1 = np.random.random_sample(size=(3,3))

print('Array1:\n',npArr1)
```

Output

```Array1:
[[0.02087227 0.56088992 0.83305691]
[0.49487638 0.51043135 0.46421906]
[0.13391219 0.67086142 0.91800014]]
```

### 5. Numpy.Uniform() to Create array of random float

In this example, we are creating a numpy array of positive and negative float ranges between -9 to 9 of size(3,3) by using the numpy library function numpy.uniform()

```import numpy as np
npArr1 = np.random.uniform((3,3))
npArr2 = np.random.uniform(-9,9,size=(3,3))

print('Array1:',npArr1)
print('\nArray2:\n',npArr2)

```

Output

```Array1: [1.96052949 1.32312375]

Array2:
[[-1.65138405  0.09615009  4.8658347 ]
[ 4.53463445 -6.33644281  0.16139375]
[-8.66366642  6.09609658 -3.2138744 ]]
[[-5.41063954 -0.6727993   0.6650472 ]
[-1.56090448 -0.15053239 -5.07475897]
[ 0.8970503  -3.0817696  -0.94034061]]
```

### 6.Uniform() function to create numpy array float range

The nump. uniform() function takes three arguments the low, high, and size, and creates an array that values are filled based on uniform distribution over the half-open interval. In this example, we are creating an array that has low =0.5,high=13.3, and size(3,3)

```import numpy as np

npArr1 = np.random.uniform(low=0.5, high=13.3, size=(3,3))

print('Array1:',npArr1)
```

Output

```Array1: [[ 8.44141064  2.2643609   7.97413926]
[ 9.06224892  2.36153455 10.33275063]
[ 1.83125795 12.3641906   8.12921322]]
```

### Summary

In this post, We have learned how to create numpy array of random values with examples by using numpy library built-in functions