打印骰子滚动全部6面的概率

问题描述 投票:1回答:1

我正在尝试打印平整模具滚动时的预期数量,并保持滚动直到所有6面都滚动。

[我想做的是,例如,当滚动一个1时,它将被添加到列表中。然后继续进行直到所有6个面都滚动为止,并使用count+=1继续滚动下一个模具,并使用count作为次数。然后,一旦列表等于[1,2,3,4,5,6],则使stop等于True并中断。

但是我应该使用shift方法,以便如果找到3,则将其从列表中删除?

然后一旦完成,我想使用count计算预期的掷骰数

import random
def rolldice():
    count = 0
    while True:
        die = []
        win = []
        for i in range(1):
            die.append(random.choice([1,2,3,4,5,6]))
        stop = False
        for roll in die:
            number = die.count(roll)
            if(number == 1):
                win += [1]
            if(number == 2):
                win += [2]
            if(number == 3):
                win += [3]
            if(number == 4):
                win += [4]
            if(number == 5):
                win += [5]
            if(number == 6):
                win += [6]
            if(win == [1,2,3,4,5,6]):
                stop = True
                break
        if stop:
            break
        else:
            count += 1
    print(f'Count is {count}')
def main():
    rolldice()
main()

试图查看我是否在正确的轨道上,还是应该使用移位和删除。

python probability dice
1个回答
2
投票

[如果您只想计算获得全部六个面所需的掷骰数,则使用一组更容易:

import random

# initialize to empty set
results = set()

# remember how many rolls we have made
rolls = 0

# keep going until we roll all six sides
while len(results) != 6:
    rolls += 1
    results.add(random.choice([1,2,3,4,5,6]))

print('It took %d rolls to get all six sides' % rolls)
© www.soinside.com 2019 - 2024. All rights reserved.