I want to create a random normal distribution with a given mean and std.
-
see this for all distributions (so you can do something like `r2 = torch.torch.distributions.Uniform(low=lb, high=ub).sample((num_samples,Din)) `): https://pytorch.org/docs/stable/distributions.html#torch.distributions.uniform.Uniform – Charlie Parker Jul 15 '20 at 16:48
6 Answers
You can easily use torch.Tensor.normal_() method.
Let's create a matrix Z (a 1d tensor) of dimension 1 × 5
, filled with random elements samples from the normal distribution parameterized by mean = 4
and std = 0.5
.
torch.empty(5).normal_(mean=4,std=0.5)
Result:
tensor([4.1450, 4.0104, 4.0228, 4.4689, 3.7810])

- 505
- 4
- 12
-
1Why if I compute the std and mean of the vector you posted, I obtained: `(tensor(4.0856), tensor(0.2514))`? Isn't the std supposed to be close to 0.5? – muammar Jun 22 '21 at 19:21
-
@muammar are you sure you are computing the standard deviation and not the variance? – kmf Apr 26 '22 at 18:34
-
@vaporK I think I am sure. Look at this: https://www.calculator.net/standard-deviation-calculator.html?numberinputs=4.1450%2C+4.0104%2C+4.0228%2C+4.4689%2C+3.7810&ctype=p&x=97&y=22 – muammar Apr 26 '22 at 22:59
-
@muammar 0.2514 is not unusual since there are only 5 values. The standard error (0.5 / sqrt(5)) is 0.22, so 68% of the time you'll get a value between 0.276 and 0.723. If you change that 5 to a 5,000, the SE drops to 0.007 and you can expect a value much closer to 0.5. – David Gilbertson Sep 25 '22 at 22:25
For a standard normal distribution (i.e. mean=0
and variance=1
), you can use torch.randn()
For your case of custom mean
and std
, you can use torch.distributions.Normal()
Init signature:
tdist.Normal(loc, scale, validate_args=None)Docstring:
Creates a normal (also called Gaussian) distribution parameterized byloc
andscale
.Args:
loc (float or Tensor): mean of the distribution (often referred to as mu)
scale (float or Tensor): standard deviation of the distribution (often referred to as sigma)
Here's an example:
In [32]: import torch.distributions as tdist
In [33]: n = tdist.Normal(torch.tensor([4.0]), torch.tensor([0.5]))
In [34]: n.sample((2,))
Out[34]:
tensor([[ 3.6577],
[ 4.7001]])

- 57,311
- 13
- 161
- 150
A simple option is to use the randn
function from the base module. It creates a random sample from the standard Gaussian distribution. To change the mean and the standard deviation you just use addition and multiplication. Below I create sample of size 5 from your requested distribution.
import torch
torch.randn(5) * 0.5 + 4 # tensor([4.1029, 4.5351, 2.8797, 3.1883, 4.3868])

- 2,768
- 3
- 18
- 23
-
Hello, I find following confusing: According to the PyTorch documentation: torch.rand returns a tensor filled with random numbers from a uniform distribution on the interval [0,1) https://pytorch.org/docs/stable/torch.html#torch.rand. I saw some developers use the same code as you to create Gaussian distribution. I would appreciate if you can please elaborate further on this? Thank you – Zvezda May 13 '20 at 23:19
-
1@Zvezda I am not sure what your question is. The `rand` function indeed samples from a uniform distribution but the `randn` function samples from a Gaussian https://pytorch.org/docs/stable/torch.html#torch.randn. Multiplying by the standard deviation and adding the mean is standard statistical practice. – gui11aume May 14 '20 at 16:50
-
2Hello, @gui11aume, thank you a lot. I realized I didn't notice these two are two different functions - rand and randN. – Zvezda May 14 '20 at 22:26
You can create your distribution like described here in the docs. In your case this should be the correct call, including sampling from the created distribution:
from torch.distributions import normal
m = normal.Normal(4.0, 0.5)
s = m.sample()
If you want to get a sample of a certain size/shape, you can pass it to sample()
, for example
s = m.sample([5, 5])
for a 5x5-Tensor.

- 1,151
- 10
- 16
It depends on what you want to generate.
For generating standard normal distribution use -
torch.randn()
for all all distribution (say normal, poisson or uniform etc) use
torch.distributions.Normal()
or torch.distribution.Uniform()
.
A detail of all these methods can be seen here - https://pytorch.org/docs/stable/distributions.html#normal
Once you define these methods you can use .sample method to generate the number of instances. It also allows you to generates a sample_shape shaped sample or sample_shape shaped batch of samples if the distribution parameters are batched.

- 445
- 6
- 15
For all distribution see: https://pytorch.org/docs/stable/distributions.html#
click on right menu to jump to normal (or search in the docs).
An example code:
import torch
num_samples = 3
Din = 1
mu, std = 0, 1
x = torch.distributions.normal.Normal(loc=mu, scale=std).sample((num_samples, Din))
print(x)
For details on torch distributions (with emphasis on uniform) see my SO answer here: https://stackoverflow.com/a/62919760/1601580

- 5,884
- 57
- 198
- 323