This example shows how to create an array of random floating-point numbers that are drawn from a normal distribution having a mean of 500 and variance of 25.

The `randn`

function returns a sample of
random numbers from a normal distribution with mean 0 and variance
1. The general theory of random variables states that if *x* is
a random variable whose mean is $${\mu}_{x}$$ and
variance is $${\sigma}_{x}^{2}$$,
then the random variable, *y*, defined by $$y=ax+b,$$where *a* and *b* are
constants, has mean $${\mu}_{y}=a{\mu}_{x}+b$$ and
variance $${\sigma}_{y}^{2}={a}^{2}{\sigma}_{x}^{2}.$$ You
can apply this concept to get a sample of normally distributed random
numbers with mean 500 and variance 25.

First, initialize the random number generator to make the results in this example repeatable.

```
rng(0,'twister');
```

Create a vector of 1000 random values drawn from a normal distribution with a mean of 500 and a standard deviation of 5.

a = 5; b = 500; y = a.*randn(1000,1) + b;

Calculate the sample mean, standard deviation, and variance.

stats = [mean(y) std(y) var(y)]

stats = 499.8368 4.9948 24.9483

The mean and variance are not 500 and 25 exactly because they are calculated from a sampling of the distribution.

Was this topic helpful?