【发布时间】:2019-03-04 23:15:54
【问题描述】:
import random
sample_size = int(input("Enter the number of times you want me to roll the die: "))
if (sample_size <=0):
print("Please enter a positive number!")
else:
counter1 = 0
counter2 = 0
final = 0
while (counter1<= sample_size):
dice_value = random.randint(1,6)
if ((dice_value) == 6):
counter1 += 1
else:
counter2 +=1
final = (counter2)/(sample_size) # fixing indention
print("Estimation of the expected number of rolls before pigging out: " + str(final))
这里使用的逻辑是否正确?它将重复掷骰子,直到掷出一个骰子,同时跟踪在出现一个骰子之前所用的掷骰数。当我运行高值(500+)时,它给出的值为 0.85
谢谢
【问题讨论】:
-
是的,这是这个计算的合理解决方案,是的,它应该输出大约 0.83 (5/6)。
-
@PM2Ring 是的,我现在已经编辑了代码。
-
final = (counter2)/(sample_size)没有正确缩进。如果不使用变量“final”,它的目的是什么。你在哪里定义和初始化expect? -
@yoonghm 它应该是“最终的”(存储在出现 1 之前所用的滚动数总和的变量)而不是“期望”。道歉。
-
你能解释一下
final = (counter2)/(sample_size)的逻辑吗?
标签: python python-3.x probability dice