-1

I am a Python beginner. I'm trying to write a simple but neat code to solve the following problem.

I've written this, but it always evaluates to ~325% somehow. What might be wrong?

import random numberOfStreaks = 0 flip = [] for experimentNumber in range(10000): # Code that creates a list of 100 'heads' or 'tails' values. for i in range(101): flip.append(random.randint(0,1)) # Code that checks if there is a streak of 6 heads or tails in a row. for i in flip: if flip[i] == flip[i+1]: if flip[i] == flip[i+2]: if flip[i] == flip[i+3]: if flip[i] == flip[i+4]: if flip[i] == flip[i+5]: numberOfStreaks += 1 flip = [] print('Chance of streak: %s%%' % (numberOfStreaks / 100)) 
2
  • Your denominator isn't 100, it's the number of experiments. That would give you ~3.25%, which is close to the expected value. Commented Apr 7, 2020 at 16:12
  • I've given an answer on a similar thread: stackoverflow.com/a/70176317/17555691 Commented Nov 30, 2021 at 21:59

2 Answers 2

0

This is happening because you are dividing numberOfStreaks by 100 and not the size of the total population.

If you want to get the proportion of streaks that occurred in your sample, then you have to divide the number of successes by the population size.

In other words...

numberOfExperiments = 10000 print('Chance of streak: {}'.format(numberOfStreaks / numberOfExperiments)) 
Sign up to request clarification or add additional context in comments.

Comments

0

Solution to coin flip streaks from "Automate the boring stuff with Python"

We won't always get the same result , simply because there is a ton of different combinations for 10000 coin flips , our list will be different each time we run the code, but the percentage of streaks that we will find each time will be around the same. Even the mathematical solution finds an approximate ( won't post it here, too complex and I doubt it will help you)

from random import randint streak = 0 expList = [] numberOfStreaks = 0 for experimentNumber in range(10000): # Code that creates a list of 10000 'heads' or 'tails' values. if randint(0,1) == 0: coin_flip = 'H' else: coin_flip = 'T' expList.append(coin_flip) for i in range(len(expList)): if i==0: pass # we don't do nothing when i=0 , otherwise we would get an error elif expList[i] == expList[i-1]: # checks if current list item is the same as before streak += 1 # best way I found so we count 7 Head or Tails in row else: # as 1 steak and not 2 streak = 0 if streak == 6: #checks if there is a streak of 6 heads or tails in a row. numberOfStreaks += 1 print (numberOfStreaks) print('Chance of streak: %s%%' % ((numberOfStreaks / 10000)*100)) # priting and finding percentage of the coin flips that # contain a streak of six heads or tails in a row #probability percentage = ( (no. of successful results) / (no. of all possible results) ) * 100% 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.