生成随机数列表,总和为 1 [英] Generating a list of random numbers, summing to 1

查看:31
本文介绍了生成随机数列表,总和为 1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 此问题与获取总和为 M 的 N 个随机数 不同,因为:

    1. 大多数答案都是关于理论的,而不是 Python 中的特定编码解决方案来回答这个问题
    2. 此处接受的答案比回答此问题的副本中的一个答案早 5 年.
    3. 重复接受的答案没有回答这个问题


我如何制作 N 个(比如 100 个)随机数的列表,使它们的总和为 1?

我可以制作一个随机数列表

r = [ran.random() for i in range(1,100)]

我将如何修改它以使列表总和为 1(这是用于概率模拟).

解决方案

最简单的解决方案确实是取 N 个随机值并除以总和.

更通用的解决方案是使用 Dirichlet 分布这是 在 numpy 中可用.

通过改变分布的参数,你可以改变随机性"个人号码

<预><代码>>>>将 numpy 导入为 np, numpy.random>>>打印 np.random.dirichlet(np.ones(10),size=1)[[ 0.01779975 0.14165316 0.01029262 0.168136 0.03061161 0.090465870.19987289 0.13398581 0.03119906 0.17598322]]>>>打印 np.random.dirichlet(np.ones(10)/1000.,size=1)[[ 2.63435230e-115 4.31961290e-209 1.41369771e-212 1.42417285e-1880.00000000e+000 5.79841280e-143 0.00000000e+000 9.85329725e-0059.99901467e-001 8.37460207e-246]]>>>打印 np.random.dirichlet(np.ones(10)*1000.,size=1)[[ 0.09967689 0.10151585 0.10077575 0.09875282 0.09935606 0.100936780.09517132 0.09891358 0.10206595 0.10283501]]

根据主要参数,狄利克雷分布将给出所有值都接近 1./N 的向量,其中 N 是向量的长度,或者给出向量的大部分值都为 ~0,并且会有一个 1,或者在这些可能性之间给出一些东西.

编辑(在原始答案之后 5 年):关于狄利克雷分布的另一个有用的事实是,如果您生成一组 Gamma 分布的随机变量,然后将它们除以它们的总和,那么您自然会得到它.

  • This question is not a duplicate of Getting N random numbers whose sum is M because:

    1. Most answers there are about theory, not a specific coding solution in python to answer this question
    2. The accepted answer here is 5 years older than the one answer in the duplicate that answers this question.
    3. The duplicate accepted answer does not answer this question


How would I make a list of N (say 100) random numbers, so that their sum is 1?

I can make a list of random numbers with

r = [ran.random() for i in range(1,100)]

How would I modify this so that the list sums to 1 (this is for a probability simulation).

解决方案

The simplest solution is indeed to take N random values and divide by the sum.

A more generic solution is to use the Dirichlet distribution which is available in numpy.

By changing the parameters of the distribution you can change the "randomness" of individual numbers

>>> import numpy as np, numpy.random
>>> print np.random.dirichlet(np.ones(10),size=1)
[[ 0.01779975  0.14165316  0.01029262  0.168136    0.03061161  0.09046587
   0.19987289  0.13398581  0.03119906  0.17598322]]

>>> print np.random.dirichlet(np.ones(10)/1000.,size=1)
[[  2.63435230e-115   4.31961290e-209   1.41369771e-212   1.42417285e-188
    0.00000000e+000   5.79841280e-143   0.00000000e+000   9.85329725e-005
    9.99901467e-001   8.37460207e-246]]

>>> print np.random.dirichlet(np.ones(10)*1000.,size=1)
[[ 0.09967689  0.10151585  0.10077575  0.09875282  0.09935606  0.10093678
   0.09517132  0.09891358  0.10206595  0.10283501]]

Depending on the main parameter the Dirichlet distribution will either give vectors where all the values are close to 1./N where N is the length of the vector, or give vectors where most of the values of the vectors will be ~0 , and there will be a single 1, or give something in between those possibilities.

EDIT (5 years after the original answer): Another useful fact about the Dirichlet distribution is that you naturally get it, if you generate a Gamma-distributed set of random variables and then divide them by their sum.

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

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