NumPy provides randint()
function to generate random integers between two values. The randint()
function is present in a random
module of NumPy.
The syntax of randint()
function is
numpy.random.randint(low, high, size)
It takes three arguments:
low
: It is the lower bound. All the numbers generated from randint()
are greater than or equal to low
.high
: It is the upper bound. All the numbers generated from randint()
are less than high
.size
: It is used to specify the shape of the returned NumPy array. If nothing is specified, then a single value is generated. You can pass an integer or a tuple of integers.In mathematical terms, numbers generated by numpy.random.randint()
function belongs to [low, high)
interval.
Note: The size
parameter controls how many numbers will be generated by randint()
.
The following code will generate a 1 dimensional NumPy array that contains 5 random integers between 0 and 100.
import numpy as np arr = np.random.randint(0, 100, size=5) print(arr)
Output
[80 8 99 86 34]
The following code will generate a 1 dimensional NumPy array that contains 6 random integers between 10 and 30.
import numpy as np arr = np.random.randint(10, 30, size=(6)) print(arr)
Output
[11 17 23 22 10 23]
The following code will generate a 3x4 NumPy array that contains 12 random integers between 0 and 10.
import numpy as np arr = np.random.randint(0, 10, size=(3, 4)) print(arr)
Output
[[0 2 3 6] [2 7 5 3] [4 7 7 3]]