使用python列表的硬币条纹概率

2024-05-16 18:13:05 发布

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

我试着编写一个程序,找出随机生成的头尾列表中出现六个头尾或六个尾巴的条纹的频率,但我的结果是荒谬的(我得到了150%,而其他人得到了80%左右)。我试图将我的程序与其他程序进行比较,但始终不明白哪里出了问题。你能告诉我怎么了吗

import random
TotalStreaks = 0
for experimentNumber in range(10000):
    # Code that creates a list of 100 'heads' or 'tails' values.
    explist = []
    numberOfStreaks = 0
    for value in range(100):
        if random.randint(0,1) == 0:
            explist.append('H')
        else:
            explist.append('T')

    # Code that checks if there is a streak of 6 heads or tails in a row.
    for i in range(95): #we are comparing the value of the next 5 values so we want the range to be 100-5

        #compare the element i with the next five elements to see if they match

        if explist[i] == explist[i+1] == explist[i+2] == explist[i+3] == explist[i+4] == explist[i+5]:

            #see if it's the first streak (a.k.a. numberOfStreaks == 0) or
            #if i is not equal to the 5 indexes that follow the most recent index of a streak 

            if numberOfStreaks == 0 or i not in rep:
                numberOfStreaks+=1

                #rep is the list of the indexes of the 5 numbers following the number i
                #which is where the streak was identified

                rep = [i+1, i+2, i+3, i+4, i+5]
                
    TotalStreaks += numberOfStreaks
                
print('Chance of streak: %s%%' % ((TotalStreaks / 10000) * 100))

Tags: orofthetoin程序forif
1条回答
网友
1楼 · 发布于 2024-05-16 18:13:05

我认为问题在最后一句话中

如果使用以下语句更改最后一条语句:

print('Chance of streak: {:.2f}%'.format((TotalStreaks / 10000) * 100))

我还在代码中做了一些小改动,但没有改变逻辑

import random

rep = []
TotalStreaks = 0
for experimentNumber in range(10000):
    # Code that creates a list of 100 'heads' or 'tails' values.
    explist = []
    numberOfStreaks = 0
    for value in range(100):
        if random.randint(0, 1) == 0:
            explist.append('H')
        else:
            explist.append('T')

    # Code that checks if there is a streak of 6 heads or tails in a row.
    for i in range(95):  # we are comparing the value of the next 5 values so we want the range to be 100-5

        # compare the element i with the next five elements to see if they match

        if explist[i] == explist[i + 1] == explist[i + 2] == explist[i + 3] == explist[i + 4] == explist[i + 5]:

            # see if it's the first streak (a.k.a. numberOfStreaks == 0) or
            # if i is not equal to the 5 indexes that follow the most recent index of a streak

            if numberOfStreaks == 0 or i not in rep:
                numberOfStreaks += 1

                # rep is the list of the indexes of the 5 numbers following the number i
                # which is where the streak was identified
                
                rep.append(i + 1)
                rep.append(i + 2)
                rep.append(i + 3)
                rep.append(i + 4)
                rep.append(i + 5)

    TotalStreaks += numberOfStreaks

print('Chance of streak: {:.2f}%'.format((TotalStreaks / 10000) * 100))

以下是输出:

Chance of streak: 80.61%

相关问题 更多 >