Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Choosing n numbers with fixed sum

In some code I want to choose n random numbers in [0,1) which sum to 1.

I do so by choosing the numbers independently in [0,1) and normalizing them by dividing each one by the total sum:

numbers = [random() for i in range(n)]
numbers = [n/sum(numbers) for n in numbers]

My "problem" is, that the distribution I get out is quite skew. Choosing a million numbers not a single one gets over 1/2. By some effort I've calculated the pdf, and it's not nice.

Here is the weird looking pdf I get for 5 variables:

enter image description here

Do you have an idea for a nice algorithm to choose the numbers, that result in a more uniform or simple distribution?

like image 402
Thomas Ahle Avatar asked Apr 11 '11 14:04

Thomas Ahle


1 Answers

You are looking to partition the distance from 0 to 1.

Choose n - 1 numbers from 0 to 1, sort them and determine the distances between each of them.

This will partition the space 0 to 1, which should yield the occasional large result which you aren't getting.

Even so, for large values of n, you can generally expect your max value to decrease as well, just not as quickly as your method.

like image 127
LanceH Avatar answered Sep 23 '22 21:09

LanceH