Skip to main content

How to Create a Tensor of Random Numbers in PyTorch with torch.rand

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
torch.rand(*size, dtype=None, device=None)
PartWhat it means
*sizeThe 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
dtypeOptional, given by name. A decimal data type. Left out, it's PyTorch's default, torch.float32
deviceOptional, 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 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​

rand.py
import torch

print(torch.rand(3))
print(torch.rand(2, 4))

The rand.py script in a macOS-style code window: print of torch.rand(3) and 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​

Terminal
uv run python rand.py
Output
tensor([0.8551, 0.9091, 0.5114])
tensor([[0.1664, 0.4103, 0.4128, 0.3047],
[0.8293, 0.0507, 0.6253, 0.2822]])

The terminal after uv run python rand.py: a row of three random decimals and a two-by-four table of random decimals, all between 0 and 1.

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​

Terminal
uv run python rand.py
Output
tensor([0.4659, 0.6348, 0.4692])
tensor([[0.5174, 0.7576, 0.0618, 0.6081],
[0.3624, 0.8337, 0.2570, 0.3154]])

The terminal after running rand.py a second time: a different row of three random decimals and a different two-by-four table.

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​

MistakeWhat happensDo this instead
Expecting whole numberstorch.rand gives decimals between 0 and 1torch.randint, as the FAQ shows
Expecting 1 as a possible valueThe 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 runEach run drew different numbersSet 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​