NumPy Tutorial NumPy Statistics NumPy References

NumPy - random.randint() function



The NumPy random.randint() function returns random integers drawn from low (inclusive) to high (exclusive), in a given shape.

Syntax

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

Parameters

low Required. Specify lowest (signed) integer to be drawn from the distribution (unless high=None, in which case this parameter is one above the highest such integer).
high Optional. If provided, one above the largest (signed) integer to be drawn from the distribution (see above for behavior if high=None).
size Optional. Specify output shape. If the given shape is, e.g., (m, n, k), then m * n * k samples are drawn. Default is None, in which case a single value is returned.
dtype Optional. Specify desired dtype of the result.

Return Value

Returns random int values from the appropriate distribution in given shape, or a single such random int if size not provided.

Example:

In the example below, random.randint() function is used to generate random integers in a given shape. As high=None in this example, samples are drawn from [0, low).

import numpy as np

x = np.random.randint(3, size=(10))
y = np.random.randint(3, size=(3, 3))

#printing x
print("x =", x)

#printing y
print("y =")
print(y)

The output of the above code will be:

x = [2 1 2 2 2 0 0 2 1 0]
y =
[[1 0 1]
 [0 0 0]
 [2 2 0]]

Example:

When high is provided, integer samples are drawn from [low, high).

import numpy as np

x = np.random.randint(3, 10, (10))
y = np.random.randint(3, 10, (3, 3))

#printing x
print("x =", x)

#printing y
print("y =")
print(y)

The output of the above code will be:

x = [9 7 4 3 6 3 3 9 4 7]
y =
[[7 6 7]
 [4 6 8]
 [4 3 4]]

❮ NumPy - Random