# How to generate two groups of $n$ random numbers in $U(0,1)$ such that sum of these two groups equal?

I want to have two groups of $n$ random numbers $u_i$ and $v_i$ in $U(0,1)$, such that $\sum u_i = \sum v_i$

What I tried is:

I can firstly get $u_i$ by $U\sim U(0,1)$, make $s=\sum u_i$.

Then I found it is very difficult to generate another $n$ uniformly distributed random numbers $v_i$ from $U(0,1)$ that sum to $s$, where $s$ can be any real value in $[0,n]$

Try to make the question clearer, my original problem is:

The random variables, of course, is not independent! But my question requires the sampled values for each parameter roughly distributed in range [0,1] such that the Monte Carlo sampling will effectively go over the whole parameter space for the system.

I have $8$ parameters $\kappa_i, i=1,\ldots,8$ from a system, each parameter $\kappa_i$ can be any value (better be uniformly distributed) in $[0,1]$. But I have a constraint on my parameters which is $\kappa_1+\kappa_2+\kappa_3+\kappa_4=\kappa_5+\kappa_6+\kappa_7+\kappa_8$. Now I want to sample the whole parameter space (is this counted as Monte Carlo?) with such constraint. What should I do?

• You want samples of numbers $\kappa_i$ such that $\sum_{i=1}^4 \kappa_i = \sum_{i=5}^8 \kappa_i$. It sounds like you want the marginal distribution of each $\kappa_i$ to be uniform on $[0, 1]$. But what conditions do you want to impose on their joint distribution? Feb 6 '16 at 15:50
• There appears to be an interesting and important question here, but it is asked in a very confusing way. Please edit it to address the comments.
– whuber
Feb 6 '16 at 16:14
• I think it is crucial what kind of dependence you allow for. Your constraint implies a dependence structure, so the four samples $\kappa_i: 1 \leq i \leq 8$ cannot be independent from each other. (To see this, note that if all $\kappa_i$ are random and independent, the probability of your constraint holding is equal to $0$.) 'How independent' do they have to be? Which dependence structure do you allow for? Feb 6 '16 at 18:10
• It's on hold so I can't post this as a solution but why not first sample $\kappa_1,\kappa_2,\kappa_3,\kappa_5,\kappa_6,\kappa_7\sim U(0,1)$. Then simulate one more $\kappa_i\sim U(0,1)$ and add it to whichever set $A=\{\kappa_1,\kappa_2,\kappa_3\}$ or $B=\{\kappa_5,\kappa_6,\kappa_7\}$ makes it such that $A>B$ or $B>A$. So either $i=4$ and $\kappa_4$ gets added to $A$ or $i=8$ and $\kappa_8$ gets added to $B$. Then, for whichever $\kappa$ is left, set the last $\kappa_8=\kappa_1+\kappa_2+\kappa_3+\kappa_4-(\kappa_5+\kappa_6+\kappa_7)$ or (ctd)... Feb 6 '16 at 19:54
• The problem of partitioning a set into two equal-in-sum (/as equal as possible) subsets is the partition problem, but it's not quite clear to me how your conditions on the distribution are supposed to work. Feb 7 '16 at 10:30

Here is a procedure to generate two $n$tuples of random variables $(u_k)$ and $(v_k)$, both i.i.d. uniform on $(0,1)$, with $u_1+\cdots u_n=v_1+\cdots+v_n$.

For every $k$, let $f_k$ denote the PDF of the sum of $k$ i.i.d. random variables uniformly distributed on $(0,1)$. Generate $n$ i.i.d. random variables $u_i$ uniform on $(0,1)$, and consider $s_n=u_1+\cdots+u_n$. Then generate $v_n$ distributed as $u_n$ conditionally on $u_1+\cdots+u_n=s_n$, that is, with density $$f_n(s_n)^{-1}\cdot f_{n-1}(s_n-x)\cdot\mathbf 1_{(0,1)}(x).$$ Next, define $s_{n-1}=s_n-v_n$ and generate $v_{n-1}$ distributed as $u_{n-1}$ conditionally on $u_1+\cdots+u_{n-1}=s_{n-1}$, that is, with density $$f_{n-1}(s_{n-1})^{-1}\cdot f_{n-2}(s_{n-1}-x)\cdot\mathbf 1_{(0,1)}(x),$$ and so on, until $v_2$ distributed as $u_2$ conditionally on $u_1+u_2=s_2$, that is, with density $$f_2(s_2)^{-1}\cdot f_{1}(s_2-x)\cdot\mathbf 1_{(0,1)}(x)=f_2(s_2)^{-1}\cdot \mathbf 1_{(s_2-1,s_2)}(x)\cdot\mathbf 1_{(0,1)}(x).$$ Finally define $v_1=s_2-v_2$. Then $(v_k)$ is i.i.d. uniform on $(0,1)$ and $u_1+\cdots u_n=v_1+\cdots+v_n$ almost surely.

To sum up, the procedure is exact but it requires to compute $n-1$ PDFs, each PDF $f_k$ being a polynomial of degree $k-1$ on each interval $(i-1,i)$ with $1\leqslant i\leqslant k$.

• Can you elaborate on the notation for the $f_n(s_n)$ and $f_{n-1}(s_{n-1})^{-1}$, and / or in general computing the pdf $f_k$? Feb 14 '16 at 21:19
• @AntoniParellada The factor is $$\frac1{f_{n-1}(s_{n-1})}.$$
– Did
Feb 16 '16 at 5:39

I have 8 parameters $κ_i,i=1,…,8$ from a system, each parameter $κ_i$ can be any value (better be uniformly distributed) in $[0,1]$. But I have a constraint on my parameters which is $κ_1+κ_2+κ_3+κ_4=κ_5+κ_6+κ_7+κ_8$. Now I want to sample the whole parameter space (is this counted as Monte Carlo?) with such constraint. What should I do?

a) simulate freely 4 random numbers [0, 1]
b) let be its sum n1 c) simulate THREE ALSO FREELY and add, say n2
d) if 0<= ni-n2 <= 1 then keep x=n1-n2, otherwise reject the attempt
e) repeat from a) to d) any times in order to get a large stock

• many TIMES (NOT ANY) Feb 14 '16 at 16:13
• There appear to be several problems with this proposal, not least of which are (1) the numbers won't usually sum to unity and (2) the asymmetric treatment of the last one leads to an unexpected marginal distribution.
– whuber
Feb 14 '16 at 16:27
• I'm afraid that doesn't fix the problems I mentioned.
– whuber
Feb 14 '16 at 16:29
• Please stop the words in capitals and rather concentrate on the mathematical point @whuber made.
– Did
Feb 14 '16 at 18:56
• There is actually a neat little exam question for introductory courses on theoretical probability here. The text of the question could read as follows. Consider seven independent random variables $T$, $U$, $V$, $W$, $X$, $Y$, $Z$, and compute the distribution of $S=(U+V+W+T)-(X+Y+Z)$ conditionally on the event $[0<S<1]$. Check that this distribution is not the uniform distribution on $(0,1)$. Hint: Show that $S+3$ is (unconditionally) distributed like $U+V+W+T+X+Y+Z$. // @OP This page might interest you.
– Did
Mar 7 '16 at 18:10