# How to generate a random data set with determined expected value and variance? [closed]

Recently I am doing my math homework and there is a question asking me to Come up with a dataset X that has at least 10 distinct numbers in it such that Expect[X] = 3.7, and Var[X] = 2.1. Actually I did not know how to use Mathematica before, so I really have no idea how to generate. I read many sources and wrote a code X=RandomVariate[NormalDistribution [3.7, Sqrt [2.1]], 10] Unfortunately it does not work...Can anyone tell me what's wrong? Thanks a lot!

-

## closed as too localized by Michael E2, Dr. belisarius, m_goldberg, Sjoerd C. de Vries, Yves KlettJun 24 '13 at 7:19

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

What's wrong is that RandomVariate is going to give a random answer. The bigger the sample, the more likely that μ will be closer to 3.7 and σ will be closer to Sqrt[2.1]. – Michael E2 Jun 24 '13 at 0:35
What do you mean by "Unfortunately it does not work." ? What do you see as output? – Dr. belisarius Jun 24 '13 at 0:59
@zzy I believe what you want is a set with exact mean and variance. I believe Mathematica won't produce what you want, once the RandomVariate[] function works better for a large set of random numbers... – Rod Jun 24 '13 at 1:07

There is a systematic way to approach this. First generate any old set of ten numbers:

x = RandomReal[{-1, 1}, 10]


Obviously they don't have the right mean or STD. So first force it to have mean zero:

z = x - Mean[x]


Now force the STD of z to be what you want:

stdNorm = Sqrt[2.1]/StandardDeviation[z]


So stdNorm is the factor by which you can multiply z so that it wil have the right STD. Check:

StandardDeviation[stdNorm z]


which gives 1.44914, a numerical approximation to Sqrt[2.1]. But the mean is still wrong. This can be adjusted by creeating the process:

new = stdNorm z + 3.7


Now check that Mean[new] and StandardDeviation[new] are the desired values. Here it is:

{Mean[new], StandardDeviation[new]}
{3.7, 1.44914}

-

If you are allowed to use a standard calculator, the point of the exercise is for you, by means of trial and error, to understand how changing a data value affects the mean and variance. (Otherwise, the point would also be to practice using the formulas for mean and variance.) Mathematica can solve the problem without you having to go through such an instructive process, but instead of showing you how to do that, I will show you tools for experimenting the way I think the problem intends you to do.

Enter this:

data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}


and this:

Dynamic @ N @ {Mean[data], Variance[data]}

{5.5, 9.16667}


Now change the numbers in the list for data and press shift-return. The mean and the variance will be automatically updated. Change data until you bring the mean and variance to the desired values. It may be hard to hit them exactly, but perhaps the book intends for you to find data such that mean and variance round off to the desired values.

-