获取N个总和为M的随机数 [英] Getting N random numbers whose sum is M
问题描述
我想获得N个随机数字,其总和是一个值.
I want to get N random numbers whose sum is a value.
例如,假设我要5个总和为1的随机数.
For example, let's suppose I want 5 random numbers that sum to 1.
然后,一种有效的可能性是:
Then, a valid possibility is:
0.2 0.2 0.2 0.2 0.2
另一种可能性是:
0.8 0.1 0.03 0.03 0.04
以此类推.在创建模糊C均值的所有物矩阵时,需要用到此.
And so on. I need this for the creation of a matrix of belongings for Fuzzy C-means.
推荐答案
简短回答:
仅生成N个随机数,计算它们的和,然后将每个除以 总和乘以M.
Just generate N random numbers, compute their sum, divide each one by the sum and multiply by M.
更长的答案:
上述解决方案不不会产生均匀分布,这取决于这些随机数的用途,可能是一个问题. Matti Virkkunen提出的另一种方法:
The above solution does not yield an uniform distribution which might be an issue depending on what these random numbers are used for. Another method proposed by Matti Virkkunen:
生成0到1之间的N-1个随机数,将数字0和1相加 自己列出来,对它们进行排序,并采用以下差异 相邻的数字.
Generate N-1 random numbers between 0 and 1, add the numbers 0 and 1 themselves to the list, sort them, and take the differences of adjacent numbers.
我不确定这是否会产生均匀分布
I'm not sure if this yield an uniform distribution
这篇关于获取N个总和为M的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!