fn

randint

Tensor
randint(low: _int, high: _int, size: list[_int] | tuple[_int, ...], dtype: DTypeLike = None, device: DeviceLike = None, requires_grad: _bool = False, generator: _C_engine.Generator | None = None)
source

Return a tensor of samples drawn from the discrete uniform distribution.

Each element is drawn independently and uniformly from the integer set {l,l+1,,h1}\{l, l+1, \ldots, h-1\}, i.e. the discrete uniform distribution:

XiU{l,h1},P(Xi=k)=1hl  k{l,,h1}X_i \sim \mathcal{U}\{l,\, h-1\}, \quad P(X_i = k) = \frac{1}{h - l} \quad \forall\; k \in \{l, \ldots, h-1\}

The distribution has mean E[X]=l+h12\mathbb{E}[X] = \tfrac{l + h - 1}{2} and variance Var[X]=(hl)2112\operatorname{Var}[X] = \tfrac{(h-l)^2 - 1}{12}.

Parameters

lowint
Lower bound of the interval (inclusive).
highint
Upper bound of the interval (exclusive). Must satisfy high > low.
sizelist[int] or tuple[int, ...]
Shape of the output tensor. Unlike rand / randn, size is a single required keyword-positional argument, not varargs.
dtypelucid.dtype
Integer data type. Defaults to lucid.int64.
devicestr or lucid.device
Target device — "cpu" or "metal".
requires_gradbool
Enable autograd tracking. Default: False. Gradients through integer tensors are always zero; this flag is accepted for compatibility with downstream wrappers.
generatorlucid._C.engine.Generator
Explicit generator. Defaults to the global default generator.

Returns

Tensor

Integer tensor of shape size with values in [l,h)[l, h).

Notes

The engine generates the integers via

Xi=l+Ui(hl),UiU[0,1)X_i = l + \lfloor U_i \cdot (h - l) \rfloor, \quad U_i \sim U[0, 1)

which gives an exact discrete uniform distribution with no acceptance–rejection overhead.

Common uses include:

  • Sampling mini-batch indices for data loaders
  • Generating random class labels for testing
  • Stochastic depth / drop-path masks

Examples

>>> import lucid
>>> lucid.manual_seed(0)
>>> lucid.randint(0, 10, (5,)).tolist()
[3, 6, 7, 9, 6]
Shuffle indices for a dataset of 1024 examples:
>>> idx = lucid.randint(0, 1024, (64,))  # sample 64 indices
Random binary mask:
>>> mask = lucid.randint(0, 2, (3, 4))  # values in {0, 1}