Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

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

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...