Alex Rivera | Logout

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

Asked 2011-04-06T08:57:16.203
14

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

My implementation does not support uniform distribution. :(

Edit
Report

1 Answer

3

Generate two random numbers between 0 and 1. Divide those each by 3. The third is the difference of 1 and the two random thirds:

void Main()
{
    Random r = new Random();
    double d1 = r.NextDouble() / 3.0;
    double d2 = r.NextDouble() / 3.0;
    double d3 = 1.0 - d1 - d2;
    System.Console.WriteLine(d1);
    System.Console.WriteLine(d2);
    System.Console.WriteLine(d3);
    System.Console.WriteLine(d1 + d2 + d3);
}

this outputs the following in LINQPad:

0.0514050276878934
0.156857372489847
0.79173759982226
1
answered 2011-04-06T09:00:36.937

Your Answer