How to generate three random numbers, whose sum is 1?

Just get 3 random numbers and then calculate a factor which is 1 / [sum of your numbers]. Finally multiply each of the random numbers with that factor. The sum will be 1.


This is actually a tricky question. First of all:
Daren's solution is not uniform because it does not support having two numbers > 1/3.
Simen's solution is not uniform assuming the "pick a random number" draws from a uniform distribution, but this is a bit more subtle. It is at least symmetric between the variables (i.e. the probability of [a, b, c] is the same as that of any permutation of that), but it heavily favors solutions closer to (1/3, 1/3, 1/3). Think about it this way by looking at extreme cases: (1/3, 1/3, 1/3) could have come from any (a, a, a), where a ranges from 0 through 1. (1, 0, 0), an equally valid triple, must come from (1, 0, 0).

One solution: The set of positive numbers that add to 1 form a n equilateral triangle in three-space, with coordinates (1,0,0), (0,1,0), (0,0,1). Extend that to a parallelogram -- e.g. by adding a point (1,1,-1) as the fourth point. This double's the area -- map the second area to the first, so that it suffices to pick a random point in this parallelogram.

The parallelogram can be sampled uniformly via (0,0,1) + A(1,0,-1) + B (0,1,-1), where A and B range uniformly from 0 to 1.

-A