Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # https://automatetheboringstuff.com/2e/chapter4/
- # project 2 Coin Flip Streaks ver. 1
- # I evaluate the numberOfStreaks on the sum of 6 flips in flipsList
- import random
- numberOfStreaks = 0
- runs = 10000
- flips_per_run = 100
- streakNo = 6 # how many streaks to look for
- for i in range(runs):
- # Code that creates a list of 100 'heads' or 'tails' values.
- flipsList = []
- for i in range(flips_per_run):
- flipsList.append(random.randint(0,1))
- # Code that checks if there is a streak of 6 heads or tails in a row.
- n = 0 # an index in flipsList
- # run the check for a length of six elements until the end of the list
- while n+streakNo < len(flipsList) - streakNo:
- sumvalue=0 #
- for i in range (streakNo):
- sumvalue += flipsList[n+i]
- if sumvalue == streakNo or sumvalue == 0:
- numberOfStreaks += 1
- n += 1
- print('numberOfStreaks is: ' + str(numberOfStreaks))
- print('Chance of streak: %s%%' % (numberOfStreaks / runs))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement