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,
If I got it right, I would suggest you to have the average, than generate a number below the average and then to add a number with the same distance from average to the other side. For example, average 4 has the maximum border distance of 1 from nearest limit 5, so you should generate between 3, 4, 5. If 3 is generated - put 5 next. If 5, then 3. If 4 - put 4 next. And so on 8 times.
The best way to solve it is to put it this way:
average = sum of all numbers / amount of numbers, therefor, average * amount = sum, as Michael said. Now, if your sum is not integer - you will have no solution for this one.
It means that no matter, which method to use - mine, or Michael's. The difference is Michael's method doubles randomness with larger execution time.