在掷1之前,如何计算我掷0的次数?

2024-05-18 15:47:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我曾经尝试过制作一个程序,它可以滚动一个由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)

Tags: printx1x2timesx10x11x3x12
3条回答

我不知道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)

我不确定这是否是您想要的,但是如果您使用a list,您的代码会更干净。我不太明白你在找什么,但我试图解决你的问题。如果您有更多问题,请随时发表评论:)

编辑:也许你不想重置列表?这段代码是否符合您的要求?我还不确定你想做什么。。。你知道吗

我想我现在明白你的意思了。我做了一些测试,这个代码应该可以工作。我不明白什么是所有的红军,失败者和黑人的东西,但我相信这足以让你去你想去的地方。你知道吗

from random import randint

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

# I still don't know why 16 is the number, but ok...
xs = [0]*16

threshold = 4875

in_a_row = 0

# Repeat this num_rolls times
for i in range(0, num_rolls):
    # As Sci Prog says, since this is a die, you want to generate a random number from 1 to 10000 (inclusive) I imagine
    r = randint(1, 10001)
    if r < threshold:
        in_a_row += 1
    else:
        in_a_row = 0

    if in_a_row > 0:
        xs[in_a_row - 1] += 1
        if in_a_row > 1:
            xs[in_a_row - 2] -= 1

print()
for i in range(0, 16):
    print("x{}: {}".format(i+1, xs[i]))

注意:我还没有实际测试过这段代码,所以我不确定它是否能正常工作,但我也不确定您要找什么:D希望这能有所帮助!:)(另外,这是我对stackoverflow的第一个回应,所以我不确定它是如何工作的……)

一些建议

  1. 滚动N面骰子时,请使用randint(1,N)而不是randint(0,N)。例如,一个标准的6面骰子可以产生1、2、3、4、5或6,而不是0。(对于10000面骰子,差异可以很小)

  2. 在您的定义中,您没有指定当滚动等于4875时会发生什么。

  3. 虽然概率变得很小,但在获得一个大于4875的数字之前,它可以进行多少次掷骰是没有限制的。如果要执行多个模拟,请使用dict

来模拟你的问题

from random import randint
THRESHOLD = 4875
DICE_SIZE = 10000
NB_SIMULATIONS = 100
def one_simulation():
  result = 0
  while randint(1,DICE_SIZE) < THRESHOLD:  # or <=                            
    result += 1
  return result

def simulations():
  result = {}
  max_rolls = 0
  for s in range(NB_SIMULATIONS):
    nr = one_simulation()
    result[nr] = 1 + result.get(nr,0)
    if nr > max_rolls:  
      max_rolls = nr
  #     
  for nr in range( max_rolls + 1 ):
    print ("%d: %d" % ( nr, result.get(nr,0) ) )

simulations()

相关问题 更多 >

    热门问题