# How to efficiently generate a set uniformly distributed numbers that add to $n$.

I am in need of a more generalized solution to this problem.

I have a random number generator that generates numbers from 0 to 1. Using this, I want to find $r$ numbers that add to $n$. How do I do this efficiently, and such that the numbers are uniformly distributed?

For my specific case, I need to be able to generate $6$ numbers that add up to $2\pi$.

Edit: I have thought of possibly using the same method in the other question, but multiplying the final set of numbers by $2\pi$. Would this be uniformly distributed? And if so, how can that be proved (That's just for curiosity's sake though)?

Since your random numbers must add up to n, they must be dependent, but then it doesn't make too much sense because you could just let one of them be $X \sim U([0,a])$ and the rest all be $\frac{1}{r-1}(n-X) \sim U([\frac{n-a}{r-1},\frac{n}{r-1}])$ for any $a \ge 0$. All of them are certainly uniformly distributed but quite useless as random numbers, just as in soakley's method, since there is no point in having any two or more random variables that are so directly related.

There is an easy way to do this when $r$ is even by using correlated pairs. So, for your problem, here is how:

Generate $X_1 \sim \textrm{Unif}[0,{2 \pi \over 3}]$ and $X_2 = {2 \pi \over 3} - X_1.$ Similarly, generate $X_3 \sim \textrm{Unif}[0,{2 \pi \over 3}]$ and $X_4 = {2 \pi \over 3} - X_3.$ Finally, do the same for the third pair, with $X_5 \sim \textrm{Unif}[0,{2 \pi \over 3}]$ and $X_6 = {2 \pi \over 3} - X_5.$ Every pair sums to ${2 \pi \over 3},$ and the sum of all 6 is $2 \pi$ as required.

Finally, each of the component random variables has a uniform distribution on $[0,{2 \pi \over 3}].$

Here is another way that can be generalized to $r$ numbers. I will show the construction for 6 numbers that add to 1. To see the same method with $r=5,$ look at the link in the first line of the question.

Generate $V$ as Uniform$[0,{1 \over 3}].$

Let $$W = \begin{cases} V+{1/15} \ , & \text{if} \ V \le {4/15} \\ V-{4/15} \ , & \text{if} \ V \gt {4/15} \end{cases}$$

$$X = \begin{cases} V+{2/15} \ , & \text{if} \ V \le {3/15} \\ V-{3/15} \ , & \text{if} \ V \gt {3/15} \end{cases}$$ $$Y = \begin{cases} V+{3/15} \ , & \text{if} \ V \le {2/15} \\ V-{2/15} \ , & \text{if} \ V \gt {2/15} \end{cases}$$

$$Z = \begin{cases} V+{4/15} \ , & \text{if} \ V \le {1/15} \\ V-{1/15} \ , & \text{if} \ V \gt {1/15} \end{cases}$$

$$U = 1-V-W-X-Y-Z.$$

I apologize for the naive response, but I do not understand: Why not just choose a modified form of the questioner's suggestion? Why can you not generate N numbers from the distribution, then just normalize by multiplying each by the ratio of (desired_sum/actual_sum_of_the_numbers)?

I'm not a math guy, but why won't this answer work? The best thing is that they are most certainly still uniformly distributed (Almost certainly $C \times \operatorname{Unif}[0,1]$ is $\operatorname{Unif}[0,C]$), albeit over a different region (note: just multiplying by $2\pi$ is insufficient, as there is no guarantee that the numbers will add to $1$ in fact, that would be a miracle).

• While a constant times a uniform is still uniform, in this case the sum of the numbers is a random variable. So the denominator in your ratio is a random variable, and the resultant components will not be uniform. Try it out with a small sample of 1000 realizations. Create some histograms and you will see how the components are not uniform. – soakley Aug 13 '14 at 17:24
• Would this imply that the product of uniformly-distributed numbers is not uniform? For that matter, would the sum? Does this naive example illustrate this? Assume a coin flip, 0/1. Clearly that's uniformly distributed over the uninteresting sequence of 0 and 1. Flip twice and you get the set {0/0, 0/1, 1/0, 1/1}. A+B would then yield {0, 1, 1, 2}. A*B would yield {0, 0, 0, 1}, neither of which are uniformly distributed. – Mark Gerolimatos Aug 13 '14 at 18:54
• I'm not sure about implication, but your conclusions are correct: neither the product nor the sum of uniform random variables is uniform. – soakley Aug 13 '14 at 22:05