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

user627482
  • 80
  • 6
  • How do you generate your numbers $x,y,z,w$? – kimchi lover Jan 11 '20 at 15:59
  • I'm working with C++, so I use $x$= rand()%101, but I don't know if this is what you want. – user627482 Jan 11 '20 at 16:04
  • @user627482 I think the main challenge here is that rand()%101 [*does not* lead to random uniform integers](https://stackoverflow.com/questions/16153589/generating-a-uniform-random-integer-in-c) – Maximilian Janisch Jan 11 '20 at 16:08
  • Another, and what worries me more, is that $x\le y\le z\le w$ is not likely to come about from calls to rand()%101. So you are in fact doing something other than what you described. Like sorting the numbers, or playing "go fish" until $x\le y\le z\le w$ happens, or something. – kimchi lover Jan 11 '20 at 16:11
  • But if $x,y,z,w$ are truly random numbers, does that method work? – user627482 Jan 11 '20 at 16:18

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
  • 227,898
  • 14
  • 283
  • 497