MATLAB: How to generate only four normally distributed random numbers with given mean and std. deviation

randum number generator

I am generating normally distributed random numbers using the 'randn' function for a given mean and standard deviation. Once I get the numbers, I check their mean and standard deviation, which is fairly accurate only if I am generating a large set of random numbers. I need to generate only four random numbers, and when I do that, the mean and std. deviation of these are not close to the specified mean and standard deviation. Please suggest?

Best Answer

That's statistics, it only works properly if the sample size is large enough.
With two constraints (mean, and std) only two out of your four numbers are truly random. If you really wanted to, you could generate two random numbers and then solve for the last two:
%generate two random numbers: a,b
%a+b+x+y = 4*mu %definition of mean, x and y unknown.
%gives: y = (4*mu-a-b) - x (eq. 1)
%(a-mu)^2 + (b-mu)^2 + (x-mu)^2 + (y-mu)^2 = 3*sigma^2 %one of the definition of std, (eq.2)
%substitute y from eq. 1 in eq. 2 and solve quadratic equation for x
Related Question