How to Create a Tensor of Random Numbers in PyTorch with torch.rand
Random numbers are everywhere in deep learning: a network's weights start random, and so does the order it sees its data in. For trying things out, a tensor of random values is also the quickest test input. This guide makes some with torch.rand in PyTorch 2.15.0 and runs it twice.
Syntax
torch.rand(*size, dtype=None, device=None)
| Part | What it means |
|---|---|
*size | The shape: one whole number per dimension, such as 2, 4 for two rows of four. A tuple or list, such as (2, 4), works too |
dtype | Optional, given by name. A decimal data type. Left out, it's PyTorch's default, torch.float32 |
device | Optional, given by name. Where to make the tensor, such as the GPU. Left out, it's the CPU |
PyTorch's reference says it returns "a tensor filled with random numbers from a uniform distribution on the interval [0, 1)": every value is at least 0 and below 1, and every value in between is equally likely.
The Example
A script makes a row of three random numbers and a table of two rows and four columns, and the script runs twice to compare the numbers.
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
print(torch.rand(3))
print(torch.rand(2, 4))

torch.rand(3) makes one row of three values, and torch.rand(2, 4) a table of two rows and four columns.
Step 2: Run It
uv run python rand.py
tensor([0.8551, 0.9091, 0.5114])
tensor([[0.1664, 0.4103, 0.4128, 0.3047],
[0.8293, 0.0507, 0.6253, 0.2822]])

Every value is a decimal between 0 and 1. Your numbers will differ from these. The command works unchanged in PowerShell.
Step 3: Run It Again
uv run python rand.py
tensor([0.4659, 0.6348, 0.4692])
tensor([[0.5174, 0.7576, 0.0618, 0.6081],
[0.3624, 0.8337, 0.2570, 0.3154]])

Every number changed: each run draws new ones. To get the same numbers every run, set a seed first, as How to Get the Same Random Numbers Every Run in PyTorch with torch.manual_seed shows.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Expecting whole numbers | torch.rand gives decimals between 0 and 1 | torch.randint, as the FAQ shows |
| Expecting 1 as a possible value | The values stop below 1: the interval is [0, 1) | Scale them: torch.rand(3) * 10 printed tensor([6.1635, 6.6439, 4.5865]), values from 0 to below 10 |
| Expecting the same numbers every run | Each run drew different numbers | Set a seed with torch.manual_seed |
FAQ
How do I get random whole numbers?
torch.randint(low, high, size) draws whole numbers from low up to high - 1. A dice roll five times, torch.randint(1, 7, (5,)), printed tensor([4, 5, 6, 5, 4]). Its size must be a tuple: (5,) is a tuple of one number.
Can I give the size as a tuple?
Yes: torch.rand((2, 4)).shape printed torch.Size([2, 4]), the same shape as torch.rand(2, 4).
Conclusion
torch.rand(3) and torch.rand(2, 4) made tensors of random decimals between 0 and 1, and running the script again gave different numbers every time.
Additional Resources
- torch.rand — PyTorch's reference for the function.
- torch.randint — random whole numbers.
- Reproducibility — how PyTorch's random numbers can be made repeatable.
