如何计算10个随机掷骰子总和的平均值

How to calculate the average of the sum of 10 random rolled dices

程序将掷出 10 个公平的骰子(六面),直到这些骰子的总和为质数且大于或等于 45,然后输出所有这些掷骰总和的平均值。我正在尝试使用 while 循环。这是我到目前为止所拥有的:

import random

# function to generate a random number between 1 and 6
def rollDice():
    roll = random.randint(1,6)
    return roll

for i in range(10):
    print(rollDice(), end = ' ')
# checks if the sum of the dices value is prime
def is_prime(dices):
    total_sum = sum(dices)
    for x in range(2,int(total_sum**0.5)+1):
        if total_sum % x == 0:
            return False
    return True

total_sum = 0
isPrime = False
while isPrime is False or total_sum < 45:
    dices = [rollDice() for dice in range(10)]
    total_sum = sum(dices)
    isPrime = is_prime(dices)

print('Average of the sum of the ten-rolls is {0:.2f}'.format(float(total_sum)/10))

问题是有时我 运行 程序中这 10 次掷骰的总和不大于 45,并且无论如何这些总和的平均值总是等于 4.70

输出应该是:显示 1-6 中的 10 个随机数十次掷骰的总和的平均值是...

您正在打印的骰子根本不是您在循环结束时拥有的那个。删除带有 print(rollDice()) 的行。 实在想看的话最好在代码末尾打印一下roll。

关于平均值总是 4.7 这仅仅是因为您没有太多选择 45 到 60 之间的素数(您的最大值为 10 个骰子)。可以是 47、53、59。

我现在没有计算,但很明显 59 是组合中可能性最小的(你只有 1 种可能的组合,即 9 乘以 6 + 1 乘以 5,所以概率是(1 /6)^10),而 47 似乎是最有可能的结果。