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

PyTorch · Function

torch.randint

Creates a tensor of uniformly distributed random integers in the half-open interval [low, high). The requested size determines its shape, and low defaults to zero.

How it works

The function constructs a tensor whose shape comes from size and whose entries are uniformly sampled integers in the half-open interval [low, high). If low is omitted, sampling starts at zero; optional arguments control randomness, storage, placement, output reuse, and gradient tracking.

Syntax

torch.randint(low=0, high, size, *, generator=None, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor

Parameters

NameTypeDescription
lowint, optionalLowest integer to sample; defaults to 0.
highintExclusive upper bound; one above the greatest sample.
sizetupleTuple defining the output shape.
generatortorch.Generator, optionalGenerator used for sampling.
outTensor, optionalDestination output tensor.
dtypetorch.dtype, optionalReturned tensor data type.
layouttorch.layout, optionalReturned tensor layout; defaults to torch.strided.
devicetorch.device, optionalDevice on which to create the tensor.
requires_gradbool, optionalWhether autograd records operations on the result; defaults to False.

Returns

A Tensor filled with uniformly sampled random integers in the half-open interval [low, high), with the shape specified by size.

Key points

  • Samples are uniform across the permitted integer values.
  • Pass dimensions as size, such as (batch, features), to determine the output shape.
  • low sets the smallest possible sampled value and defaults to 0.
  • high is expressed as one more than the largest value that may be produced.
  • The size tuple can describe vectors, matrices, or higher-dimensional tensors.
  • Use generator when sampling must be driven by a particular PyTorch random-number generator.

Common mistakes

  • The upper bound is excluded, so high itself can never appear; use high one greater than the desired maximum.
  • The default result type is int64 even when the global floating-point default is float32; specify dtype when a narrower integer type is appropriate.

Example: Generate bounded integer samples

import torch

samples = torch.randint(0, 10, (2, 3), dtype=torch.int32)
print(samples)
print(samples.shape, samples.dtype)

Creates a 2-by-3 tensor containing integer samples from 0 through 9.

When to use it

Categorical integer data

Create synthetic labels or token IDs within a known vocabulary range.

Randomized indexing and tests

Build randomized indices, masks, or test fixtures with controlled bounds.

Device- and dtype-aware initialization

Generate tensors directly on a selected accelerator or with an explicit integer dtype.

Interview questions

QWhat range and shape does torch.randint produce?

torch.randint returns a tensor filled with uniformly sampled integers. Values are at least low and strictly less than high, while size determines the tensor shape.

Follow-up: How would you generate values from 1 through 6? Use torch.randint(1, 7, size), because the high endpoint is exclusive.

QWhat does omitting low do?

If low is omitted, it defaults to zero, so torch.randint(10, (2, 2)) samples from 0 through 9. The required high argument is the exclusive upper bound in this form.

Follow-up: What changes if you need values 10 through 19? Call torch.randint(10, 20, size), rather than passing only 20.

QWhich keyword arguments are useful when integrating randint into a model or data pipeline?

The function accepts optional generator, out, dtype, layout, device, and requires_grad keyword arguments. A generator controls sampling, dtype selects the returned type, device selects placement, and out supplies the destination tensor.

Follow-up: Why might dtype matter for storage? The documented default is int64, so explicitly selecting a suitable integer dtype can reduce representation size when its range is sufficient.

  • torch.randint_like
  • torch.rand
  • torch.randn
  • torch.randperm

Source: torch.randint — PyTorch 2.13 documentation. Checked 2026-10-16.