Skip to main content

How to Get the Same Random Numbers Every Run in PyTorch with torch.manual_seed

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
torch.manual_seed(seed)
PartWhat it means
seedA 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 numpy adds 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​

seed.py
import torch

torch.manual_seed(7)
print(torch.rand(3))
print(torch.rand(3))

torch.manual_seed(7)
print(torch.rand(3))

The seed.py script in a macOS-style code window: torch.manual_seed(7), two prints of torch.rand(3), then torch.manual_seed(7) again and one more print of 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​

Terminal
uv run python seed.py
Output
tensor([0.5349, 0.1988, 0.6592])
tensor([0.6569, 0.2328, 0.4251])
tensor([0.5349, 0.1988, 0.6592])

The terminal after uv run python seed.py: three rows of random numbers, where the first and third rows are both 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​

Terminal
uv run python seed.py
Output
tensor([0.5349, 0.1988, 0.6592])
tensor([0.6569, 0.2328, 0.4251])
tensor([0.5349, 0.1988, 0.6592])

The terminal after running seed.py a second time: exactly the same three rows as the first run.

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​

seed_gpu.py
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 seed_gpu.py script in a macOS-style code window: the accelerator or the CPU picked as the device, then torch.manual_seed(7) and a print of torch.rand(3) made on that 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:

Terminal
uv run python seed_gpu.py
Output
tensor([0.0139, 0.2964, 0.7355], device='mps:0')
Terminal
uv run python seed_gpu.py
Output
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').

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​

MistakeWhat happensDo this instead
Setting the seed after something random already ranOnly the numbers drawn after the seed repeatSet it at the top of the script
Expecting the GPU to repeat the CPU's numbersSeed 7 gave 0.5349, 0.1988, 0.6592 on the CPU and 0.0139, 0.2964, 0.7355 on the GPUCompare runs on the same device
Expecting the same numbers in another PyTorch version or on another computerPyTorch'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​