# 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

-
If x is an integer, try: x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x. Otherwise, y=x-.5 is an integer then try: y, y, y, y, y, y, y, y, y+1, y+1, y+1, y+1, y+1, y+1, y+1, y+1. – Did Mar 30 '12 at 13:18
I don't want all the numbers x. I want random numbers. – Somnath Muluk Mar 30 '12 at 13:28
x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x IS a random sample. You will not find any other sample whose probability to occur would be greater. – Did Mar 30 '12 at 16:00

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.

-
I have updated question what I mean. – Somnath Muluk Mar 30 '12 at 13:29

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.

-

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...

-
I think this works nicely except that it is more convenient to translate the set to go from 0 to 4. With $x_{1} + x_{2} + x_{3} = 9$, it is equivalent to choosing a random number from 1-3 9 times (for each ball we choose the bin it will go into). So in general for n numbers between a and b with average x, first find nx as the overall total we want (round it in any way we want etc).Now let N = nx - na. Generate numbers from 0 to (b-a) N times, and keep a count for the number of times each of these bins got generated. Add na to each of these frequencies and you get the random numbers you need. – Wonder Mar 30 '12 at 13:45
I meant 'add a to each of these frequencies', not 'add na' but I am not able to edit the comment any more. Not sure how this works. – Wonder Mar 30 '12 at 13:53

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)$.

-