gpt4 book ai didi

python - 如何计算在掷出 1 之前掷出 0 的次数?

转载 作者:太空宇宙 更新时间:2023-11-04 08:50:32 24 4
gpt4 key购买 nike

我试过制作一个程序,该程序会掷一个由 10000 个面组成的骰子(想象一下)并计算掷出的数字在超过 4875 之前连续低于 4875 的次数。代表的数字这样的数字连续滚动了多少次不应该大于 16。我无法让显示这种事情连续发生多少次的部分工作。我的代码非常困惑,因为我更像是一个 HTML/CSS 人而不是程序员,而且我仍然是 Python 新手……提前致谢!

 import random

userRolls = int(input("Rolls: "))
print("")

reds = 0
blacks = 0
lost = 0
compRolls = 0
times = 0

x1 = 0
x2 = 0
x3 = 0
x4 = 0
x5 = 0
x6 = 0
x7 = 0
x8 = 0
x9 = 0
x10 = 0
x11 = 0
x12 = 0
x13 = 0
x14 = 0
x15 = 0
x16 = 0

while compRolls < userRolls:

compGen = random.randint(0,10000)
if compGen < 4875:
compGen = 0
elif compGen > 5125:
compGen = 1
else:
lost = lost + 1

print(compGen)

if compGen == 0:
reds = reds + 1
times = times + 1
else:
blacks = blacks + 1
times = 0

if times == 1:
x1 = x1 + 1
x2 = 0
x3 = 0
x4 = 0
x5 = 0
x6 = 0
x7 = 0
x8 = 0
x9 = 0
x10 = 0
x11 = 0
x12 = 0
x13 = 0
x14 = 0
x15 = 0
x16 = 0
elif times == 2:
x2 = x2 + 1
x1 = 0
x3 = 0
x4 = 0
x5 = 0
x6 = 0
x7 = 0
x8 = 0
x9 = 0
x10 = 0
x11 = 0
x12 = 0
x13 = 0
x14 = 0
x15 = 0
x16 = 0
elif times == 3:
x3 = x3 + 1
x2 = 0
x1 = 0
x4 = 0
x5 = 0
x6 = 0
x7 = 0
x8 = 0
x9 = 0
x10 = 0
x11 = 0
x12 = 0
x13 = 0
x14 = 0
x15 = 0
x16 = 0
elif times == 4:
x4 = x4 + 1
x2 = 0
x3 = 0
x1 = 0
x5 = 0
x6 = 0
x7 = 0
x8 = 0
x9 = 0
x10 = 0
x11 = 0
x12 = 0
x13 = 0
x14 = 0
x15 = 0
x16 = 0
elif times == 5:
x5 = x5 + 1
x2 = 0
x3 = 0
x4 = 0
x1 = 0
x6 = 0
x7 = 0
x8 = 0
x9 = 0
x10 = 0
x11 = 0
x12 = 0
x13 = 0
x14 = 0
x15 = 0
x16 = 0
elif times == 6:
x6 = x6 + 1
x2 = 0
x3 = 0
x4 = 0
x5 = 0
x1 = 0
x7 = 0
x8 = 0
x9 = 0
x10 = 0
x11 = 0
x12 = 0
x13 = 0
x14 = 0
x15 = 0
x16 = 0
elif times == 7:
x7 = x7 + 1
x2 = 0
x3 = 0
x4 = 0
x5 = 0
x6 = 0
x1 = 0
x8 = 0
x9 = 0
x10 = 0
x11 = 0
x12 = 0
x13 = 0
x14 = 0
x15 = 0
x16 = 0
elif times == 8:
x8 = x8 + 1
x2 = 0
x3 = 0
x4 = 0
x5 = 0
x6 = 0
x7 = 0
x1 = 0
x9 = 0
x10 = 0
x11 = 0
x12 = 0
x13 = 0
x14 = 0
x15 = 0
x16 = 0
elif times == 9:
x9 = x9 + 1
x2 = 0
x3 = 0
x4 = 0
x5 = 0
x6 = 0
x7 = 0
x8 = 0
x1 = 0
x10 = 0
x11 = 0
x12 = 0
x13 = 0
x14 = 0
x15 = 0
x16 = 0
elif times == 10:
x10 = x10 + 1
x2 = 0
x3 = 0
x4 = 0
x5 = 0
x6 = 0
x7 = 0
x8 = 0
x9 = 0
x1 = 0
x11 = 0
x12 = 0
x13 = 0
x14 = 0
x15 = 0
x16 = 0
elif times == 11:
x11 = x11 + 1
x2 = 0
x3 = 0
x4 = 0
x5 = 0
x6 = 0
x7 = 0
x8 = 0
x9 = 0
x10 = 0
x1 = 0
x12 = 0
x13 = 0
x14 = 0
x15 = 0
x16 = 0
elif times == 12:
x12 = x12 + 1
x2 = 0
x3 = 0
x4 = 0
x5 = 0
x6 = 0
x7 = 0
x8 = 0
x9 = 0
x10 = 0
x11 = 0
x1 = 0
x13 = 0
x14 = 0
x15 = 0
x16 = 0
elif times == 13:
x13 = x13 + 1
x2 = 0
x3 = 0
x4 = 0
x5 = 0
x6 = 0
x7 = 0
x8 = 0
x9 = 0
x10 = 0
x11 = 0
x12 = 0
x1 = 0
x14 = 0
x15 = 0
x16 = 0
elif times == 14:
x14 = x14 + 1
x2 = 0
x3 = 0
x4 = 0
x5 = 0
x6 = 0
x7 = 0
x8 = 0
x9 = 0
x10 = 0
x11 = 0
x12 = 0
x13 = 0
x1 = 0
x15 = 0
x16 = 0
elif times == 15:
x15 = x15 + 1
x2 = 0
x3 = 0
x4 = 0
x5 = 0
x6 = 0
x7 = 0
x8 = 0
x9 = 0
x10 = 0
x11 = 0
x12 = 0
x13 = 0
x14 = 0
x1 = 0
x16 = 0
elif times == 16:
x16 = x16 + 1
x2 = 0
x3 = 0
x4 = 0
x5 = 0
x6 = 0
x7 = 0
x8 = 0
x9 = 0
x10 = 0
x11 = 0
x12 = 0
x13 = 0
x14 = 0
x15 = 0
x1 = 0
else:
pass

compRolls = compRolls + 1

print("")
print("x1: ", x1)
print("x2: ", x2)
print("x3: ", x3)
print("x4: ", x4)
print("x5: ", x5)
print("x6: ", x6)
print("x7: ", x7)
print("x8: ", x8)
print("x9: ", x9)
print("x10: ", x10)
print("x11: ", x11)
print("x12: ", x12)
print("x13: ", x13)
print("x14: ", x14)
print("x15: ", x15)
print("x16: ", x16)
print("")
print("Reds: ", reds)
print("Blacks: ", blacks)
print("Lost: ", lost)

最佳答案

我不确切知道数字 16 在这方面有何重要意义,除非您可能想在 16 次掷骰后停止检查。这段代码应该这样做:

import random

sides = 10000
threshold = 4875
num_rolls = 0
r = int(random.random() * sides)

while r < threshold and num_rolls < 16:
num_rolls = num_rolls + 1
r = int(random.random() * sides)

print(num_rolls)

关于python - 如何计算在掷出 1 之前掷出 0 的次数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35832033/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com