Skip to content
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)

Implementing kernel

C++ engine symbols that back this Python API.

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,…,h−1}\{l, l+1, \ldots, h-1\}, i.e. the discrete uniform distribution:

Xi∼U{l, h−1},P(Xi=k)=1h−l∀  k∈{l,…,h−1}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+h−12\mathbb{E}[X] = \tfrac{l + h - 1}{2} and variance Var⁡[X]=(h−l)2−112\operatorname{Var}[X] = \tfrac{(h-l)^2 - 1}{12}.

Parameters

lowint
Lower bound of the interval (inclusive). Leave it out for the reference's two-argument spelling randint(high, size), which draws from [0, high).
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= None
Integer data type. Defaults to lucid.int64.
devicestr or lucid.device= None
Target device — "cpu" or "metal".
requires_gradbool= False
Enable autograd tracking. Default: False. Gradients through integer tensors are always zero; this flag is accepted for compatibility with downstream wrappers.
Explicit generator. Defaults to the global default generator.

Returns

Tensor

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

Raises

lucid._C.engine.InvalidArgument
high <= low, or dtype cannot hold low or high - 1 (int8 past 127): such a draw used to wrap round.

Notes

The engine generates the integers via Xi=l+⌊Ui⋅(h−l)⌋X_i = l + \lfloor U_i \cdot (h - l) \rfloor for Ui∼U[0,1)U_i \sim U[0, 1), giving an exact discrete uniform distribution with no acceptance–rejection overhead. Common uses include sampling mini-batch indices, generating random class labels, and stochastic depth masks.

Examples

>>> import lucid
>>> _ = lucid.manual_seed(0)
>>> x = lucid.randint(0, 10, (5,))
>>> x.shape, x.dtype
((5,), lucid.int64)
>>> bool(((x >= 0) & (x < 10)).all())
True
>>> lucid.randint(10, (5,)).shape  # high only: values in [0, 10)
(5,)
>>> idx = lucid.randint(0, 1024, (64,))  # sample 64 indices
>>> mask = lucid.randint(0, 2, (3, 4))  # values in {0, 1}