Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

NumPy · Function

np.random.randint

Generates random integers from a discrete uniform distribution over a half-open interval. Supply one bound for values in [0, low), or two bounds for values in [low, high).

How it works

The function samples integers from a discrete uniform distribution. With both bounds supplied, values are at least low and strictly less than high; when high is omitted, low acts as the exclusive upper bound and zero is the lower bound. The size argument controls whether the result is scalar or an array, while dtype selects the integer type.

Syntax

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

Parameters

NameTypeDescription
lowint or array-like of intsLower bound of the interval; included when high is supplied. If high is omitted, this is the exclusive upper bound and the lower bound is zero.
highint or array-like of ints, optionalOptional exclusive upper bound. If omitted, sampling uses the interval [0, low).
sizeint or tuple of ints, optionalOptional output shape. None returns a scalar; an integer or tuple of integers requests an array.
dtypedtype, optionalOptional dtype of the generated integers; defaults to int.

Returns

An integer when size is None, otherwise an ndarray of random integers with the requested size and dtype.

Key points

  • With two bounds, low is included and high is excluded.
  • Omitting high changes the call to a zero-based range ending before low.
  • size accepts either one dimension or a complete output-shape tuple.
  • Leaving size unset returns one random integer rather than an ndarray.
  • dtype chooses the integer type used for the generated result.
  • When size is provided, the result is an array matching that requested shape.

Common mistakes

  • The upper bound is exclusive, so high itself can never be returned.
  • A one-argument call uses low as the exclusive upper limit and samples from zero upward.
  • A tuple passed as size determines the output dimensions and total number of generated samples.

Example: Generate bounded integer samples

import numpy as np

samples = np.random.randint(1, 7, size=(2, 4), dtype=np.int64)
print(samples)
# Every value is an integer from 1 through 6.

Creates an array of eight random six-sided-die values arranged as two rows of four.

When to use it

Bounded discrete sampling

Simulating dice, indexes, or categorical labels within a bounded integer range.

Array generation

Creating reproducible-shape test fixtures or synthetic integer matrices.

Random index and identifier selection

Selecting random positions or IDs when the valid domain is represented by consecutive integers.

Interview questions

QWhat interval does np.random.randint generate?

It samples from a discrete uniform distribution over a half-open interval. With high provided, the interval is [low, high); without it, the interval is [0, low).

Follow-up: How would you generate values in an inclusive interval [a, b]? Use np.random.randint(a, b + 1), provided b + 1 is valid for the chosen integer dtype.

QHow does size affect the result?

size may be an integer or a tuple of integers. An integer produces a one-dimensional array of that length, a tuple produces that shape, and None returns one random integer.

Follow-up: What determines the number of samples for size=(m, n, k)? The product m*n*k.

QWhat does dtype control?

dtype specifies the desired result dtype and defaults to int. The returned value is either a single integer or an array whose shape follows size.

Follow-up: Why might dtype matter? It controls the integer representation and therefore the range and storage characteristics of the result.

  • numpy.random.Generator.integers
  • numpy.random.random_integers
  • numpy.random.choice
  • numpy.random.uniform

Source: numpy.random.randint. Checked 2026-10-15.