Generating a data set with given mean and variance Suppose we have to create n integers  in a given range say between 1 and 1000 with given mean and variance .My question is :Is there an algorythm that can tell us whether such a data set exists and how to create it if it exists?Thanks in advance for any help.
 A: Let $p_1, p_2, ..., p_n$ be unknown reals, say $n=1000$, and let $M\ge0$, and $s^2$ be the required mean and variance, respectively.
The following conditions are to be met:
$$\sum_{k=1}^n p_i=1$$
$$\ \ \ \sum_{k=1}^n ip_i=M$$
$$\ \ \ \sum_{k=1}^n (i-M)^2p_i =s^2$$
with the restriction that
$$\ 0\le p_i \le 1$$ for all $i$.
You probably can solve this system of equations for $n=1000$. Choose 997 $p_i$'s between $0$ and $1$ for which
$$\sum_{k=1}^{997} p_i\lt1,$$
and
$$\sum_{k=1}^{997} ip_i\lt M.$$
and
$$\sum_{k=1}^{997} (i-M)^2p_i \lt s^2.$$
Then try to solve the remaining three equations. If there are solutions between $0$ and $1$ then you are OK, if not then choose new $p_i$'s.
Finally take the following intervals: 
$$[0,p_1), \ [p_1,p_1+p_2), \ [p_1+p_2, p_1+p_2+p_3),\  ..., \ [\sum_{k=1}^{999} p_i,1].$$
Then get an ordinary random number generator (RAND) that will give you independent random numbers uniformly distributed over the interval $[0,1]$.
If RAND falls in the $i^{th}$ interval defined above then your numer is $i$. These randomly selecten integers will have the desired mean and variance.
