Rate this Page
★ ★ ★ ★ ★

torch.func._random.bits#

torch.func._random.bits(key, *shape, dtype=None)[source]#

Generate raw random bits from a PRNG key.

Equivalent to randint() over the full range of dtype. That range is a power of two, so unlike a bounded draw this is exactly uniform. Signed dtypes receive negative values as well.

Dtypes narrower than 32 bits take the low bits of a full 32-bit sample, so bits(key, shape, dtype=torch.int8) matches bits(key, shape, dtype=torch.int32).to(torch.int8).

Supports batched keys: if key has shape (*batch, K), the leading dimensions of shape must be broadcastable with *batch and each key independently generates its slice of the output.

Parameters:
  • key (Tensor) – A PRNG key returned by key(), split(), or fold_in().

  • *shape (int) – The desired output shape.

  • dtype (torch.dtype, optional) – The desired dtype: any integer dtype other than torch.bool. Default: torch.int32.

Returns:

A tensor of the given shape filled with raw random bits.

Return type:

Tensor

Example:

>>> key = torch.func._random.key(42, device="cuda")  
>>> torch.func._random.bits(key, (1000,), dtype=torch.uint64)