How to generate random matrix in Numpy?

Let’s see how to generate random matrix using the NumPy library in Python.

numpy random array

Using Random Rand method

To generate Numpy matrix populated with random numbers use random Numpy module.

import numpy as np

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

print(random_array)

numpy random array

As you can see, the rand function syntax requires you to provide just the number of rows and columns.

See also  How to Generate a 3D Meshgrid Array in Numpy

Using Random Randint method

However, there is also the possibility to generate an array in a different way. You can populate your array with integers within a specified range using the randint function.

import numpy as np

random_array = np.random.randint(0, 7, size=10)

print(random_array)

numpy random array range

Based on this code you probably know. 0 is low value and 7 is max one. Size of array is 10.

See also  How to create histogram in Matplotlib and Numpy the easiest way?