在 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/