Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I need to generate 3 random numbers, the amount of which is equal to 1.

My implementation does not support uniform distribution. :(

like image 529
antigravity Avatar asked Apr 06 '11 08:04

antigravity


1 Answers

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

like image 62
Anirvan Mukherjee Avatar answered Oct 12 '22 10:10

Anirvan Mukherjee