How to create a matrix of random integers in Python ? (original) (raw)

Last Updated : 11 Dec, 2020

Prerequisites: numpy

To create a matrix of random integers in Python, randint() function of the numpy module is used. This function is used for random sampling i.e. all the numbers generated will be at random and cannot be predicted at hand.

Syntax : numpy.random.randint(low, high=None, size=None, dtype=’l’)

Parameters :

Return : Array of random integers in the interval [low, high) or a single such random int if size not provided.

Example 1:

Python3

import numpy as np

array = np.random.randint( 10 , size = ( 20 ))

print (array)

Output:

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

Example 2:

Python3

import numpy as np

array = np.random.randint( 10 , size = ( 2 , 3 ))

print (array)

Output:

[[8 6 7] [2 9 9]]

Example 3:

Python3

import numpy as np

array = np.random.randint( 2 , size = ( 5 , 5 ))

print (array)

Output:

[[0 0 1 0 0]

[1 0 1 1 0]

[0 1 0 1 0]

[0 1 0 0 1]

[0 1 0 1 0]]

Similar Reads