---
title: seed
description: >-
  A context manager that resets the global random state on exit to what it was
  before entering.
canonical: >-
  https://lightning.ai/docs/pytorch/stable/optional-api/api/lightning.pytorch.utilities.seed
---
# seed {#seed}

Functions

- [`isolate_rng`](#lightning.pytorch.utilities.seed.isolate_rng) — A context manager that resets the global random state on exit to what it was before entering.

Utilities to help with reproducibility of models.

### `lightning.pytorch.utilities.seed.isolate_rng(include_cuda=True)` {#lightning.pytorch.utilities.seed.isolate_rng}

A context manager that resets the global random state on exit to what it was before entering.

It supports isolating the states for PyTorch, Numpy, and Python built-in random number generators.

Parameters:

**include\_cuda** ([`bool`](https://docs.python.org/3/builtins/functions.html#bool "(in Python v3.15)")) – Whether to allow this function to also control the torch.cuda random number generator. Set this to `False` when using the function in a forked process where CUDA re-initialization is prohibited.

Return type:

[`Generator`](https://docs.python.org/3/library/collections.abc.html#collections.abc.Generator "(in Python v3.15)")\[[`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)"), [`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)"), [`None`](https://docs.python.org/3/builtins/constants.html#None "(in Python v3.15)")\]

Example

```python
>>> import torch
>>> torch.manual_seed(1)  
<torch._C.Generator object at ...>
>>> with isolate_rng():
...     [torch.rand(1) for _ in range(3)]
[tensor([0.7576]), tensor([0.2793]), tensor([0.4031])]
>>> torch.rand(1)
tensor([0.7576])
```

---

**Navigation**

- Parent: [Api](/docs/pytorch/2.6.6/optional-api/api.md)
