torch.random — PyTorch 2.7 documentation (original) (raw)

torch.random.fork_rng(devices=None, enabled=True, _caller='fork_rng', _devices_kw='devices', device_type='cuda')[source][source]

Forks the RNG, so that when you return, the RNG is reset to the state that it was previously in.

Parameters

Return type

Generator

torch.random.get_rng_state()[source][source]

Returns the random number generator state as a torch.ByteTensor.

Note

The returned state is for the default generator on CPU only.

See also: torch.random.fork_rng().

Return type

Tensor

torch.random.initial_seed()[source][source]

Returns the initial seed for generating random numbers as a Python long.

Note

The returned seed is for the default generator on CPU only.

Return type

int

torch.random.manual_seed(seed)[source][source]

Sets the seed for generating random numbers on all devices. Returns atorch.Generator object.

Parameters

seed (int) – The desired seed. Value must be within the inclusive range[-0x8000_0000_0000_0000, 0xffff_ffff_ffff_ffff]. Otherwise, a RuntimeError is raised. Negative inputs are remapped to positive values with the formula0xffff_ffff_ffff_ffff + seed.

Return type

Generator

torch.random.seed()[source][source]

Sets the seed for generating random numbers to a non-deterministic random number on all devices. Returns a 64 bit number used to seed the RNG.

Return type

int

torch.random.set_rng_state(new_state)[source][source]

Sets the random number generator state.

Note

This function only works for CPU. For CUDA, please usetorch.manual_seed(), which works for both CPU and CUDA.

Parameters

new_state (torch.ByteTensor) – The desired state