Synthetic tensor data
Generate random input tensors for tests, demonstrations, or synthetic data pipelines.
PyTorch · Function
torch.rand creates a tensor of a requested shape with uniformly distributed random values in the half-open interval [0, 1). Optional arguments control the generator, output properties, device, gradient tracking, and memory allocation.
torch.rand creates a new tensor whose dimensions come from the positional size arguments. Each element is sampled independently from a uniform distribution over the half-open interval [0, 1), while keyword options control generation, storage, placement, and autograd behavior.
torch.rand(*size, *, generator=None, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False, pin_memory=False) → Tensor| Name | Type | Description |
|---|---|---|
size | int... | One or more integers specifying the output dimensions. |
generator | torch.Generator, optional | Optional pseudorandom number generator used for sampling. |
out | Tensor, optional | Optional destination tensor for the operation. |
dtype | torch.dtype, optional | Optional element dtype; defaults to the global default dtype when omitted. |
layout | torch.layout, optional | Optional tensor layout; defaults to torch.strided. |
device | torch.device, optional | Optional placement; otherwise follows the current default tensor device. |
requires_grad | bool, optional | Whether autograd records operations on the returned tensor; default false. |
pin_memory | bool, optional | Whether to allocate pinned memory; supported only for CPU tensors and defaulting to false. |
A Tensor filled with uniformly distributed random numbers in [0, 1).
import torch
generator = torch.Generator().manual_seed(7)
x = torch.rand(2, 3, generator=generator, dtype=torch.float64)
print(x.shape) # torch.Size([2, 3])
print(bool(((x >= 0) & (x < 1)).all())) # TrueCreates a reproducible 2-by-3 tensor of float64 samples and verifies that every value lies in the documented interval.
Generate random input tensors for tests, demonstrations, or synthetic data pipelines.
Create initialized tensors whose values are bounded between zero and one.
Use a seeded Generator to make experiments and tests repeatable.
Allocate random tensors directly on a selected device or with a selected dtype.
torch.rand accepts one or more integer dimensions through size and returns a tensor filled with samples from a uniform distribution on [0, 1). The upper endpoint is excluded.
Follow-up: How would you obtain normally distributed values instead? Use torch.randn, which is the related random-sampling operation for a standard normal distribution.
The positional size arguments define the output shape, so torch.rand(2, 3) produces a two-dimensional tensor with shape 2 by 3. Keyword arguments can then specify dtype, layout, device, and other properties.
Follow-up: How can shape and properties be inherited from an existing tensor? Use torch.rand_like, which creates uniform samples with the input's size and can preserve relevant tensor attributes.
generator supplies the pseudorandom number generator used for sampling, allowing callers to control the random stream. In practice, seeding a torch.Generator makes repeated calls reproducible under the same conditions.
Follow-up: What option controls gradient tracking on the result? Set requires_grad=True when autograd should record operations involving the returned tensor.
torch.rand_liketorch.randntorch.randn_liketorch.randinttorch.randpermtorch.set_default_dtypetorch.set_default_device