Table of Contents
Shortcuts

# torch.random

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

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

  Parameters:

    - devices (iterable of Device IDs) – devices for which to fork the RNG. CPU RNG state is always forked. By default, `fork_rng()` operates on all devices, but will emit a warning if your machine has a lot of devices, since this function will run very slowly in that case. If you explicitly specify devices, this warning will be suppressed

    - enabled (bool) – if `False`, the RNG is not forked. This is a convenience argument for easily disabling the context manager without having to delete it and unindent your Python code under it.

    - deivce_type (str) – device type str, default is cuda. As for custom device, see details in [Note: support the custom device with privateuse1]

  Return type:

    Generator

torch.random.get_rng_state()

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

  Return type:

    Tensor

torch.random.initial_seed()

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

  Return type:

    int

torch.random.manual_seed(seed)

  Sets the seed for generating random numbers. Returns a torch.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 formula 0xffff_ffff_ffff_ffff + seed.

  Return type:

    Generator

torch.random.seed()

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

  Return type:

    int

torch.random.set_rng_state(new_state)

  Sets the random number generator state.

  Parameters:

    new_state (torch.ByteTensor) – The desired state

## Docs

Access comprehensive developer documentation for PyTorch

View Docs

## Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

## Resources

Find development resources and get your questions answered

View Resources

To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: Cookies Policy.