7
$\begingroup$

I am trying to simulate 4 random numbers that sum to 100.

My original ideas the following:

  1. Generate $x$: $$ x \sim \text{Uniform}(0, 100) $$

  2. Generate $ y $: $$ y \sim \text{Uniform}(0, 100 - x) $$

  3. Generate $ z $: $$ z \sim \text{Uniform}(0, 100 - x - y) $$

  4. Generate $ w $: $$ w \sim \text{Uniform}(0, 100 - x - y - z) $$

  5. Sum of the four numbers: $$ \text{Sum} = x + y + z + w \leq 100 $$

However, I realized that there is no guarantee that these 4 numbers will sum to 100!

I saw this an R simulation:

enter image description here

enter image description here

Is there a better way to do this? Can the Dirichlet Distribution be used? (https://en.wikipedia.org/wiki/Dirichlet_distribution)

$\endgroup$
2
  • 6
    $\begingroup$ The distributions over $x, y, z, w$ in this approach are also wildly asymmetric. It would be easier to generate $x, y, z, w, \sim U(0, 100)$, or anything else, then normalize their sum to $100$; that would at least guarantee symmetry. The Dirichlet distribution could also be used and would also guarantee symmetry. It depends on what you're trying to model. $\endgroup$ Commented Aug 9 at 21:50
  • $\begingroup$ Does random number mean integer? And does "random numbers sum to 100" mean you are looking for a uniform distribution of tuples among all tuples that sum to 100? $\endgroup$
    – qwr
    Commented Aug 11 at 5:05

2 Answers 2

9
$\begingroup$

To sample four random numbers summing to $100$, you can use the Dirichlet distribution with $k=4$ and $\alpha=(1,1,1,1)$. Here is an easy way to sample from this distribution, according to wikipedia.

  • First, choose three numbers $A_1,A_2,A_3\sim \text{Uniform}(0,100)$, independently.

  • Let $B_1,B_2,B_3$ be the result of sorting $A_1,A_2,A_3$ in increasing order. So, $B_1=\min(A_1,A_2,A_3),B_2=\text{median}(A_1,A_2,A_3)$ and $B_3=\text{max}(A_1,A_2,A_3)$.

  • Finally, define $$ X=B_1,\quad Y=B_2-B_1,\quad Z=B_3-B_2,\quad W=100-B_3. $$

Clearly, $X+Y+Z+W=100$ always. You can check that $X,Y,Z,$ and $W$ all have the same probability distribution. The common cdf of these four random variables is $$ F(x)=P(X\le x)=1-(1-x/100)^3. $$


Here is a complicated method to accomplish the same task. We will choose four numbers $X,Y,Z,W$ such that $X+Y+Z+W=2$ always, and such that each variable has the same distribution. This time, the common distribution will be uniform, $X\sim \text{Unif}(0,1)$. If you then scale up everything by a factor of $50$, you then get four random variables summing to $100$, where each is uniform on $(0,50)$.

Let $x_1,x_2,x_3,\dots$ be the sequence of base-$4$ digits of $X$. This means that $x_i\in \{0,1,2,3\}$ for each $i\in \{1,2,3,\dots\}$, and that $X=\sum_{i=1}^\infty x_i\cdot (1/4)^i$. Define $y_i,z_i,$ and $w_i$ similarly. We are going to choose $X,Y,Z,W$ by simultaneously choosing all of the base-$4$ digits of all four variables.

The idea is that, for each $i\ge 1$, the $4$-tuple of digits $(x_i,y_i,z_i,w_i)$ will be chosen uniformly from the $4!$ permutations of $(0,1,2,3)$. Note that $X$ will be $\text{Unif}(0,1)$, because $X$ has uniformly random base-$4$ digits. Furthermore, it will always be the case that $$ X+Y+Z+W=\sum_{i=1}^\infty (0+1+2+3)(1/4)^i=6\times \frac{1/4}{1-1/4}=2. $$

$\endgroup$
3
  • $\begingroup$ I was about to post a similar answer, but I won't now given this. The only change in my idea was to have $A1,A2,A3,A4$ and $B1,B2,B3,B4$ and define $X=(B1-B4)$ modulo $100$ and $W=B4-B3$. $Y$ and $Z$ would be the same as yours $\endgroup$
    – Srini
    Commented Aug 9 at 22:46
  • $\begingroup$ +1 Note that if you want four positive integers which sum to $100$ then you should choose $A_1,A_2,A_3$ from $\{1,2,\ldots, 99\}$ without replacement - so not quite independent of each other. $\endgroup$
    – Henry
    Commented Aug 9 at 22:55
  • $\begingroup$ @ Mike : thank you so much! Do you know if we can use the Dirichlet Probability Distribution to simulate these numbers? $\endgroup$
    – farrow90
    Commented Aug 10 at 23:50
1
$\begingroup$

You can only generate 3 of the 4 numbers, since you also have a constraint on the sum. Your best bet is to sample $x$, $y$ and $z$ from $U(0,100)$ and then define $w = 400 - (x+y+z)$, and divide each of the obtained numbers by $4$

$\endgroup$
1
  • 5
    $\begingroup$ $w$ will be biased high in this process as the expected sum of $x,y,z$ is $150$. $\endgroup$ Commented Aug 9 at 22:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .