0

I am writing a computer program which involves generating 5 random non negative integer numbers with a specific sum, namely 30.

I've found this method, but I don't know if it really generates a uniform distribution for all 5-uples.

I generate 4 random numbers $x\leq y\leq z\leq w$ between 0 and 100. Then my 5 random numbers are $$ a=x,\;b=y-x,\;c=z-y,\;d=w-z,\;e=100-w $$

Is this method correct or it gives a different distribution? For example, is (0,0,0,0,100) likely as much as (0,0,0,100,0)?

1 Answers1

1

As noted in a comment, it's not entirely clear what you mean by “I generate $4$ random numbers $x\leq y\leq z\leq w$ between $0$ and $100$”. I’ll assume that what you mean is that you independently uniformly randomly generate $4$ numbers in $[0,100]$ and then sort them into that order.

By and large your approach is good, but you have a slight bias against generating $0$ because the tuples with difference $0$ are less likely to occur than other tuples. For instance, you only have one chance to generate $(0,0,0,0,100)$ (namely with $x=y=z=w=0$) but $4!=24$ chances to generate e.g. $(1,1,1,1,96)$ (namely with all permutations of $x,y,z,w=1,2,3,4$).

The accepted answer to Method of generating random numbers that sum to 100 - is this truly random? shows how to circumvent this problem – by generating ordered tuples of different numbers in $[0,104]$ instead of $[0,100]$, and subtracting $1$ from the differences.

joriki
  • 242,601