In Python, we have the random module used to generate random numbers of a given type using the PRNG algorithm. Here, we are going to discuss the list of available functions to generate a random array in Python.
Python random Array using rand
The Numpy random rand function creates an array of random numbers from 0 to 1. Here, you have to specify the shape of an array.
import numpy as np arr = np.random.rand(7) print('-----Generated Random Array----') print(arr) arr2 = np.random.rand(10) print('\n-----Generated Random Array----') print(arr2)

Python 2D Random Array
Here, we are using this random rand function to generate a random two-dimensional array.
import numpy as np arr = np.random.rand(2, 2) print('-----Generated Two Dimensional Random Array----') print(arr) arr2 = np.random.rand(4, 5) print('\n-----Generated Two Dimensional Random Array----') print(arr2)

Python 3D Random Array
Let us create a random three-dimensional array using random rand function.
import numpy as np arr = np.random.rand(2, 2, 2) print('-----Generated Three Dimensional Random Array----') print(arr) arr2 = np.random.rand(2, 4, 5) print('\n-----Generated Three Dimensional Random Array----') print(arr2)

Python random randn
The randn function generates random arrays of one, 2D, and 3D Arrays.
import numpy as np arr = np.random.randn(5) print('-----Generated Random Array----') print(arr) arr2 = np.random.randn(8) print('\n-----Generated Random Array----') print(arr2)

This time we are generating two dimensional and three-dimensional random arrays using this Numpy random randn function.
import numpy as np arr = np.random.randn(5, 4) print('-----Generated Two Dimensional Random Array----') print(arr) arr2 = np.random.randn(2, 3, 5) print('\n-----Generated Three Dimensional Random Array----') print(arr2)

Python random Array using random function
In this example, we are using the Numpy random function available in the random module to generate an array of random numbers of length 6 and 8
import numpy as np arr = np.random.random(5) print('-----Generated Random Array----') print(arr) arr2 = np.random.random((4, 5)) # Array size 4 * 5 print('\n-----Generated Two Dimensional Random Array----') print(arr2) arr3 = np.random.random((2, 3, 5)) # Array size 2 * 3 * 5 print('\n-----Generated Three Dimensional Random Array----') print(arr3)

Python random randint
The Numpy random randint function returns an integer array from low value to high value of given size. The syntax of this Numpy function in Python is.
numpy.random.randint(low, high = None, size = None, type = ‘l’)
Let us see an example
import numpy as np arr = np.random.randint(0, 5, size = 4) print('-----Generated Random Array----') print(arr) arr2 = np.random.randint(10, 50, size = (5, 10)) print('\n-----Generated Two Dimensional Random Array----') print(arr2) arr3 = np.random.randint(1, 40, size = (2, 3, 7)) print('\n-----Generated Three Dimensional Random Array----') print(arr3)

Python random normal
The Python random normal function generates random numbers from a normal distribution. This Numpy normal accepts the size of an array then fills that array with normally distributed values.
import numpy as np arr = np.random.normal(size = 4) print('-----Generated Random Array----') print(arr) arr2 = np.random.normal(size = (5, 5)) print('\n-----Generated Two Dimensional Random Array----') print(arr2) arr3 = np.random.normal(size = (2, 3, 4)) print('\n-----Generated Three Dimensional Random Array----') print(arr3)

Python random uniform
The Python random uniform function generates a uniform distribution of random numbers. This random uniform accepts the array size and fills that array with uniform distributed values.
import numpy as np arr = np.random.uniform(size = 5) print('-----Generated Random Array----') print(arr) arr2 = np.random.uniform(size = (5, 5)) print('\n-----Generated Two Dimensional Random Array----') print(arr2) arr3 = np.random.uniform(size = (2, 3, 4)) print('\n-----Generated Three Dimensional Random Array----') print(arr3)
