python - 当均值已知时,Python 中的随机数生成器

标签 python random

在 Python 中,我试图在 [0,100] 之间获取 10 个随机数的列表,这些随机数的平均值为 25。我拥有的所有信息都在下面。

Total = 250
Number_of_users = 10
Average_score = 25

过去我随机使用过高斯函数,但没有标准差,我有点卡住了。还有另一种方法吗?

我的输出是这样的:

[20, 30, 18, 21, 27, 30, 15, 24, 31, 30]

最佳答案

好吧,如果你想要如果可能的话总数也将是 250 那么答案将从 Multinomial Distribution 中抽样.根据定义,它将产生总和为 250 且均值为 25 的随机值。如果其中一个数字超过 100(这种情况非常罕见),我们将玩接受/拒绝游戏。在 NumPy 的帮助下

import numpy as np

Total = 250
Number_of_users = 10
Average_score = 25
Upper_boundary = 100

probs = np.full(10, 1.0/np.float64(Number_of_users), dtype=np.float64) # probabilities

N = 10000 # samples to test
k = 0
while k < N:
    q = np.random.multinomial(Total, probs)
    t = np.where(q > Upper_boundary) # check for out-of boundaries
    if np.any(t):
        print("Rejected, out of boundaries") # reject, do another sample
        continue
    # accepted
    # do something with q, print((sum(q), len(q), np.mean(q)))

    k += 1

关于python - 当均值已知时,Python 中的随机数生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55415799/

相关文章:

c++ - 在 C/C++ 中获取大的随机数

c++ - 生成随机顶点

python - 如何在Python中从文本文件读取input()

python - 有没有办法用Python计算一个集合的所有合法分区?

python - 使用 pynacl (Ed25519) 导入私钥对文本进行签名

c# - 基于百分比加权的选择

algorithm - 如何将一个值限制在一个范围内?

python - 通过 Tweepy 获取 Twitter 中的所有关注者 ID

python asyncio,如何从另一个线程创建和取消任务

html - 随机放置的 div,没有重叠