How to Get the Same Random Numbers Every Run in PyTorch with torch.manual_seed
PyTorch draws new random numbers every time a script runs, which makes two runs hard to compare. Setting a seed makes the draws repeat exactly. This guide sets one with torch.manual_seed in PyTorch 2.15.0, on the CPU and on the GPU of an Apple M1 Pro.
Syntax
torch.manual_seed(seed)
| Part | What it means |
|---|---|
seed | A whole number. The same seed makes PyTorch draw the same random numbers again |
PyTorch's reference says it "Sets the seed for generating random numbers on all devices", and accepts any whole number from -0x8000_0000_0000_0000 to 0xffff_ffff_ffff_ffff.
The Example
A script sets the seed 7, draws three random numbers twice, then sets the seed 7 again and draws once more. A second script does the same on the GPU.
Prerequisites
- A uv project with PyTorch and NumPy.
uv add torch numpyadds both; without NumPy, PyTorch 2.15.0 prints a warning each time it starts. This guide uses PyTorch 2.15.0 and NumPy 2.5.5 on Python 3.15.2, with uv 0.13.10, on an Apple M1 Pro.
Step 1: Write the Script
import torch
torch.manual_seed(7)
print(torch.rand(3))
print(torch.rand(3))
torch.manual_seed(7)
print(torch.rand(3))

torch.rand(3) draws three random decimals between 0 and 1. The seed is set before the first draw, then again before the last one.
Step 2: Run It
uv run python seed.py
tensor([0.5349, 0.1988, 0.6592])
tensor([0.6569, 0.2328, 0.4251])
tensor([0.5349, 0.1988, 0.6592])

The first and the third rows are the same: each came right after torch.manual_seed(7). The second row differs, because a seed doesn't freeze the numbers: it restarts the sequence PyTorch draws them from. The command works unchanged in PowerShell.
Step 3: Run It Again
uv run python seed.py
tensor([0.5349, 0.1988, 0.6592])
tensor([0.6569, 0.2328, 0.4251])
tensor([0.5349, 0.1988, 0.6592])

Every number is the same as in the first run, on this computer with this version of PyTorch.
Step 4: Do the Same on the GPU
import torch
accelerator = torch.accelerator.current_accelerator(check_available=True)
device = accelerator or torch.device("cpu")
torch.manual_seed(7)
print(torch.rand(3, device=device))

The first two lines pick the GPU when PyTorch can use one, and the CPU otherwise: How to Check Whether PyTorch Can Use a GPU (CUDA or Apple MPS) explains them. device=device draws the numbers on that device. Run it twice:
uv run python seed_gpu.py
tensor([0.0139, 0.2964, 0.7355], device='mps:0')
uv run python seed_gpu.py
tensor([0.0139, 0.2964, 0.7355], device='mps:0')
![The terminal after running seed_gpu.py twice: both runs print tensor([0.0139, 0.2964, 0.7355], device='mps:0').](https://strdewiridedevblogsprod.blob.core.windows.net/images/blog/ai/pytorch/2026/pytorch-manual-seed-05-gpu.webp)
Both runs drew the same numbers on mps:0, the M1 Pro's GPU. They're not the CPU's numbers, though, even with the same seed 7. PyTorch's reproducibility notes warn about exactly this: "results may not be reproducible between CPU and GPU executions, even when using identical seeds."
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Setting the seed after something random already ran | Only the numbers drawn after the seed repeat | Set it at the top of the script |
| Expecting the GPU to repeat the CPU's numbers | Seed 7 gave 0.5349, 0.1988, 0.6592 on the CPU and 0.0139, 0.2964, 0.7355 on the GPU | Compare runs on the same device |
| Expecting the same numbers in another PyTorch version or on another computer | PyTorch's notes say reproducible results are "not guaranteed across PyTorch releases, individual commits, or different platforms" | Compare runs with the same version on the same computer |
FAQ
Which number should I use as the seed?
Any whole number in the range above. What matters is using the same one each time you want the same numbers.
Does it also fix Python's random module?
No. torch.manual_seed seeds PyTorch's own random numbers. PyTorch's reproducibility notes set Python's separately, with random.seed(0).
Conclusion
torch.manual_seed(7) made torch.rand(3) draw 0.5349, 0.1988, 0.6592 on the CPU in every run, and 0.0139, 0.2964, 0.7355 on the M1 Pro's GPU in every run: repeatable on each device, but different between the two.
Additional Resources
- torch.manual_seed — PyTorch's reference for the function.
- Reproducibility — every source of randomness in PyTorch and how to control it.
- torch.rand — the random numbers this guide draws.
