选择n个固定和的数字 [英] Choosing n numbers with fixed sum

查看:14
本文介绍了选择n个固定和的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些代码中,我想在 [0,1) 中选择 n 个随机数,其总和为 1.

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

我通过在 [0,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]

我的问题"是,我得到的分布很不平衡.选择一百万个数字没有一个超过1/2.通过一些努力,我已经计算了 pdf,但它并不好.

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.

这是我为 5 个变量得到的奇怪的 pdf:

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

您是否有一个很好的算法来选择数字,从而产生更均匀或更简单的分布?

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

推荐答案

您正在寻找从 0 到 1 的距离划分.

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

从 0 到 1 中选择 n - 1 个数字,对它们进行排序并确定它们之间的距离.

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

这会将空间划分为 0 到 1,这应该会偶尔产生您没有得到的大结果.

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

即便如此,对于较大的 n 值,您通常也可以预期最大值也会下降,只是没有您的方法那么快.

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.

这篇关于选择n个固定和的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆