Random sampling in numpy | randint() function (original) (raw)

Last Updated : 17 Nov, 2025

The numpy.random.randint() function is used to generate random integers within a specified range. It allows you to create arrays of any shape filled with random integer values, making it useful in simulations, testing, and numerical experiments.

**Example:

**Input: Generate integers between 0 and 5
**Output: [3 1 4 0 2]

**Explanation: Each value is a random integer from the interval [0, 5).

Syntax

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

**Parameters:

Examples

**Example 1: This example generates five random integers between 0 and 4, stored in a one-dimensional array.

Python `

import numpy as np arr = np.random.randint(0, 5, size=5) print(arr)

`

**Example 2: This example creates a 2×3 matrix of random integers ranging from 0 to 9.

Python `

import numpy as np arr = np.random.randint(0, 10, size=(2, 3)) print(arr)

`

**Explanation:

**Example 3: This example produces a 3D array (2×2×4) with values between 5 and 15.

Python `

import numpy as np arr = np.random.randint(5, 15, size=(2, 2, 4)) print(arr)

`

Output

[[[ 8 7 12 12] [ 6 13 13 6]]

[[11 6 6 7] [ 7 9 6 8]]]