Get `n` random values between 2 numbers having average `x` I want to get n random numbers (e.g $n=16$)(whole numbers) between $1$ to $5$ (including both) so that average is $x$.
$x$ can be any value between $(1, \  1.5,\  2, \ 2.5,\  3,\  3.5,\  4,\  4.5,\  5)$.
I am using PHP.
e.g.
Suppose I have average $x= 3$.
Then required $16$ whole numbers between $1$ to $5$ (including both),
like $(1,\ 5,\ 3,\ 3,\ 3,\ 3,\ 2,\ 4,\ 2,\ 4,\ 1,\ 5,\ 1,\ 5,\ 3,\ 3)$
Update:
if $x=3.5$ means average of $16$ numbers should be between $3.5$ to $4$.
and if $x=4$ means average of $16$ numbers should be between $4$ to $4.5$
and if $x=5$ means all numbers are $5$
 A: Let us say you want n=3 numbers and you want the mean to be 4. This is the same as saying that you want 3 integer numbers between 1 and 5 such that their sum is 12. Of course the three of them are not "random" in the sense that if you know two of them, you know the remaining one. So, you want to choose randomly an element of the subset $\{x_1+x_2+x_3=12\}$ of the set $\{1,2,3,4,5\}^3.$ This is a well-posed problem. (Note that in this case you may not ask for the mean to be, say, 2.5 because $2.5\cdot 3$ is not an integer). 
Edit: Sorry, I've just deleted some nonsense I posted next. Need to think a bit further...
A: Let $L(n,s)$ be the set of $n$-tuples of integers in $\{1,2,\ldots,m\}$ whose sum is $s$.  The number of those is $N(n,s)$ which is the coefficient of $t^s$ in $(t^1+t^2+\ldots+t^m)^n = \left(\frac{1-t^{m+1}}{1-t}\right)^n$.  To choose a random member of $L(n,s)$, start with the first element $x_1$.  The number of members of $L(n,s)$ with first element $k$ is $N(n-1,s-k)$ for $k=1$ to $m$, so choose $x_1 = k$ with probability
$N(n-1,s-k)/N(n,m,s)$.  Then use this method recursively to choose $x_2,\ldots,x_n$ from
$L(n-1,s-x_1)$.
A: If the numbers are random you can't force their mean to be a particular value, say x, as you suggest. The mean value will vary according to the sample drawn from the set of random numbers.
If you have a uniform random number generator, which gives values between 0 and 1 say, you can make the mean tend towards x by multiplying the random numbers by 2 x.
You can shift the mean value by adding a constant y to the numbers, should you wish to do so.
A: It sounds like you want numbers to be randomly generated, but to be weighted toward a particular average.  The question is, how weighted do you want this average to be?
In other words, let's say that you want the average to be 3.  Do you want for 2.5 and 3.5 to be weighted higher than 2 and 4?  These are some considerations that you can take into account when deciding the type of distribution you'd really like to have.
In any case, this can be accomplished as follows:
Let's say that the domain of your set is as you indicated, "{1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5}," and that you'd like for 2 to occur twice as often as every other number.
To accomplish this, duplicate the number that you'd like to have occur twice as often (in this case, 2), to form a new set: {1, 1.5, 2, 2, 2.5, 3, 3.5, 4, 4.5, 5}, and then simply take a random number from this set, selected uniformly from the indices 0 to 9.  The number 2 will occur twice as often.
