# How to create a matrix of random integers in Python?

In Python, you can create a matrix of random integers using the NumPy library. NumPy is a powerful library for scientific computing in Python that provides support for multidimensional arrays, among other things.

To create a matrix of random integers using NumPy, you can use the numpy.random.randint() function. This function generates random integers between a specified range and returns a NumPy array of the specified shape. The range of integers to be generated is specified using the low and high arguments of the function.

To specify the shape of the matrix, you can use the size argument of the numpy.random.randint() function. The size argument takes a tuple that specifies the dimensions of the matrix. For example, if you want to create a 3x3 matrix, you would pass in (3, 3) as the value of the size argument.

Once you have specified the range and shape of the matrix, you can call the numpy.random.randint() function to generate a matrix of random integers. The function returns a NumPy array that can be stored in a variable.

## Syntax

Below is the syntax of the randint() function I was talking about.

numpy.random.randint(low, high=None, size=None, dtype='l')

• low − The lowest (inclusive) integer to be generated in the matrix.

• high − The highest (exclusive) integer to be generated in the matrix. If not specified, the highest integer will be one greater than the low value.

• size − The shape of the matrix to be generated as a tuple. For example, (3, 3) would generate a 3×3 matrix.

• dtype − The data type of the output array. By default, it is set to l for integers.

Now let's explore different examples on how we can create random integers in Python.

## Example

Consider the code shown below.

# First, we import the NumPy library as 'np'
import numpy as np

# Then, we use NumPy's random.randint() function to generate an array of 20 random integers between 0 and 9 (inclusive).
array = np.random.randint(low=0, high=10, size=20)

# Finally, we print the generated array to the console.
print(array)


## Explanation

• First, the NumPy library is imported with the line import numpy as np.

• The np.random.randint() function is called with the arguments low=0, high=10, and size=20. This generates an array of 20 random integers between 0 and 9 (inclusive).

• The resulting array is stored in the variable array.

• The print() function is used to print the contents of the array variable to the console.

## Output

On execution, you will get a matrix of random numbers −

[7 1 4 7 6 8 9 9 0 5 5 6 1 0 2 4 2 9 1 2]


## Example

Consider the code shown below.

# First, we import the NumPy library as 'np'
import numpy as np

# Then, we use NumPy's random.randint() function to generate a 2x3 matrix of random integers between 0 and 9 (inclusive).
array = np.random.randint(low=0, high=10, size=(2, 3))

# Finally, we print the generated matrix to the console.
print(array)


## Explanation

• The NumPy library is imported using the line import numpy as np.

• The np.random.seed() function is called with the argument 42. This sets the random seed for NumPy's random number generator, ensuring that the same random numbers are generated every time the code is run.

• The np.random.randint() function is called with the arguments low=0, high=10, and size=(3, 3). This generates a 3x3 matrix of random integers between 0 and 9 (inclusive).

• The resulting matrix is stored in the variable matrix.

• The print() function is used to print the contents of the matrix variable to the console.

## Output

On execution, you will get an output like this −

[[9 5 7]
[0 6 1]]


## Example

Now let's take another example. Consider the code shown below.

# First, we import the NumPy library as 'np'.
import numpy as np

# Then, we use NumPy's random.randint() function to generate a 5x5 matrix of random integers consisting of only 0s and 1s.
array = np.random.randint(low=0, high=2, size=(5, 5))

# Finally, we print the generated matrix to the console.
print(array)


## Explanation

• First, the NumPy library is imported with the line import numpy as np.

• The np.random.rand() function is called with the arguments 3 and 4. This generates a 3×4 matrix of random floating-point numbers between 0 and 1.

• The resulting matrix is stored in the variable matrix.

• The print() function is used to print the contents of the matrix variable to the console.

## Output

On execution, you will get a matrix of random integers −

[[1 1 1 0 1]
[0 1 0 0 0]
[0 1 0 1 0]
[0 0 1 0 1]
[0 0 1 0 1]]


## Conclusion

In conclusion, generating a matrix of random integers in Python can be done using the "np.random.randint()" function provided by the NumPy library.

Updated on: 20-Apr-2023

3K+ Views 