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

Python 如何计算10个随机掷骰子之和的平均值,python,python-3.x,sum,average,dice,Python,Python 3.x,Sum,Average,Dice,程序将掷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(rollDic

程序将掷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


输出应该是:显示10个随机数,从1到6,十个骰子的总和的平均值是…

您正在打印的骰子不是循环结束时的骰子。使用printrollDice删除行。 如果你真的想看的话,你最好在代码的末尾打印一卷

平均值总是4.7,这仅仅是因为你在45到60之间没有太多的素数选择,而你的最大值是10个骰子。它可以是47、53、59


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

我运行了几次,我也得到了不同的值。为什么你打印出10个随机卷,然后在不同的10个随机卷上进行计算?