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

232 Views Asked by At

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)?

3

There are 3 best solutions below

0
On

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}].$

0
On

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.

0
On

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