得到N个总和为M的随机数 [英] Getting N random numbers whose sum is M

查看:24
本文介绍了得到N个总和为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 a 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.

这会产生均匀分布,如此处此处

This yields a uniform distribution as is explained here

这篇关于得到N个总和为M的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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