# generate random numbers with exact mean and std

44 views (last 30 days)

Show older comments

Hello,

I want to generate training data with exact mean and standard deviation. I have some examples that i want to illustrate to students in lab. However, using basic random numbers from normal distribution does not guarantee the exact mean and std. Any help plz?

Thanks

##### 1 Comment

Oleg Komarov
on 8 Oct 2014

### Answers (4)

Michael Haderlein
on 8 Oct 2014

I agree with the two earlier posts that exact mean/std values and randomness are contradictory. However, the data seems to be meant for training purpose, so maybe something like this is actually requested:

>> x=randn(100,1);

>> [mean(x),std(x)]

ans =

-0.0706 0.9141 %random mean/std

>> x=x-mean(x);

>> [mean(x),std(x)]

ans =

0.0000 0.9141 %exact mean, random std

>> x=x/std(x);

>> [mean(x),std(x)]

ans =

0.0000 1.0000 %exact mean/std

So this is still random data, but the mean is exactly zero and the standard deviation is exactly 1 (yes, I know, after so many digits there will be nonzero values, but that's numerics).

Best regards,

Michael

##### 3 Comments

Dominic Martin
on 11 Feb 2021

John D'Errico
on 8 Oct 2014

Edited: John D'Errico
on 8 Oct 2014

(Sounds like the teacher needs a refresher course in stats.) Those samples are not truly random IF they have the exact mean & standard deviation!

If you truly want to illustrate something, explain to your students what it means to come from a random sampling, that the exact mean and standard deviation will only be achieved with an infinite number of samples. The last time I checked, an infinite number of samples will be difficult to obtain, or at least it will take a while. So show how as the sample size increases, the expected deviation decreases.

If you are still unconvinced, think about what it would mean if you always got the exact mean and standard deviation from all samplings. Take a sample of size 1. Yep, ONE single, solitary sample. What could you infer if the sample mean was always exactly the same as the population mean? And is it possible to have a non-zero standard deviation with a sample of size 1?

So, now suppose you drew a sample of size 2. 2 points from the given distribution. Hey, at least we can get the desired mean and standard deviation. But the fact is, this would EXACTLY specify the two points if you know their mean and standard deviation in advance. So a "random" sample of size 2, at least random according to your definition, would obviously not be random at all.

My point is, a sample from a distribution will only asymptotically approach the population distribution parameters as the sample size increases towards the infinite.

So teach your students what a population mean and standard deviation are and how to compute the mean & std from a sample, AND the difference between those two animals. Teach them why in general the two sets of parameters will never be identically the same.

##### 0 Comments

Iain
on 8 Oct 2014

A sequence of equal numbers of -1 and 1 has a mean of 0 and a population standard deviation of precisely 1.

That sequence of -1 and 1 can be predictable, or not.

John is partially correct in his assertion that you need an "infinite" sample size to get the true standard deviation & mean. However, as illustrated by the dataset [-1 1] and [-1 -1 1 1], it is most certainly not impossible for a subset of the "infinite" set of values to give the same mean and standard deviation.

##### 3 Comments

Michael Haderlein
on 9 Oct 2014

Edited: Michael Haderlein
on 9 Oct 2014

Ok, the order is random. You're right ;-)

Chibuzo Nnonyelu
on 15 Sep 2015

x = mu + sigma*randn(sizeof); % for normally distributed random numbers

OR

x = random('normal', mu, sigma, row, column); % they basically do the same thing.

##### 1 Comment

Walter Roberson
on 15 Sep 2015

### See Also

### Categories

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!