I need to write a program to find out how often a streak of six heads or a streak of six tails comes up in a randomly generated list of heads and tails. My program breaks up the experiment into two parts: the first part generates a list of randomly selected 'heads' and 'tails' values, and the second part checks if there is a streak in it. Put all of this code in a loop that repeats the experiment 10,000 times so we can find out what percentage of the coin flips contains a streak of six heads or tails in a row. I have already written the programming code. But as a beginner, I am not sure if it is absolutely correct. Can anyone please check it and give feedback?
def com(list):
mystr = ''
if len(list) != 0:
for i in range(len(list)):
mystr += str(list[i])
return (mystr)
import random
numberOfStreaks = 0
for experimentNumber in range(10000):
# Code that creates a list of 100 'heads' or 'tails' values.
H_T = []
Numberofvalues = 100
for x in range(Numberofvalues):
if random.randint(0,1) == 0:
H_T.append('H')
else:
H_T.append('T')
# Code that checks if there is a streak of 6 heads or tails in a row.
HT = com(H_T)
six_T = ('T')*6
six_H = ('H')*6
if six_H in HT or six_T in HT:
numberOfStreaks += 1
print('Chance of streak: %s%%' % (numberOfStreaks / 100))
Aucun commentaire:
Enregistrer un commentaire