打印骰子滚动所有 6 个面时的概率
Printing the probability when a die rolls all 6 sides
我正在尝试打印公平骰子滚动时的预期数量,并继续滚动直到所有 6 个面都滚动。
我想做的是,当 1
被滚动时,它被添加到列表中。然后继续直到所有6面都滚动并使用count+=1
继续滚动下一个骰子并使用count作为次数。然后一旦列表等于 [1,2,3,4,5,6]
使停止等于 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()
看看我是否在正确的轨道上,或者我是否应该使用移位和删除。
如果你只是想计算得到所有六个面需要多少卷,使用一组更容易:
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)
我正在尝试打印公平骰子滚动时的预期数量,并继续滚动直到所有 6 个面都滚动。
我想做的是,当 1
被滚动时,它被添加到列表中。然后继续直到所有6面都滚动并使用count+=1
继续滚动下一个骰子并使用count作为次数。然后一旦列表等于 [1,2,3,4,5,6]
使停止等于 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()
看看我是否在正确的轨道上,或者我是否应该使用移位和删除。
如果你只是想计算得到所有六个面需要多少卷,使用一组更容易:
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)