2
import random as rd

n = 0
ListOfStreaks = []
ListOfResults = []
while n != 10:
    numberOfStreaks = 0
    for i in range(100):
        Flip = rd.randint(0,1)
        ListOfResults.append(Flip)

    for i in range(96):
        count = 0
        for j in range(6):
            if ListOfResults[i] == ListOfResults[i + j]:
                count += 1
                if count == 6:
                    numberOfStreaks += 1
                    count = 0
                else:
                    continue
            else:
                break
    ListOfStreaks.append(numberOfStreaks)
    n += 1

print(ListOfStreaks)
print(len(ListOfResults))

In the code above, I am able to successfully flip a coin 100 times, and examine how many times in the 100 flips Heads or Tails came up six time in a row. I am unable to properly set up the code to run the experiment 10 times in order to examine how many times Heads or Tails came up six times in a row in each of the single experiments. The goal is to not flip the coins 1,000 times in a row but 10 experiments of flipping 100 coins in a row.

The exercise focuses on later being able to simulate the experiment 10,000 times in order to see what the probability is of Heads or Tails appearing six times in a row in 100 flips. Essentially, I am trying to gather enough of a sample size. While there are actual statistical/probability methods to get the exact answer, that isn't what I am trying to focus on.

CoinFlip Code

David Buck
  • 3,752
  • 35
  • 31
  • 35
Trey
  • 21
  • 3
  • 3
    If your code does what you want for a "single experiment" then your easiest path forward is to put it in a function and call that function 10 times. The function should return the number of streaks. – JonSG Nov 15 '21 at 14:22

1 Answers1

2

Your key problem appears to be that you have ListOfResults = [] outside of your while loop, so each run adds another 100 entries to the list instead of setting up a new test.

I've replaced the initial for loop with a list comprehension which sets up a new sample each time.

import random as rd

list_of_streaks = []

for _ in range(10):
    list_of_results = [rd.randint(0,1) for _ in range(100)]
    number_of_streaks = 0
    for i in range(96):
        if sum(list_of_results[i: i+6]) in(0, 6):
            number_of_streaks += 1
    list_of_streaks.append(number_of_streaks)

print(list_of_streaks)
print(len(list_of_results))

You also don't need the inner for loop to add up all of the 6 flips - you can just sum them to see if the sum is 6 or 0. You appear to have just tested for heads - I tested for 6 identical flips, either heads or tails, but you can adjust that easily enough.

It's also much easier to use a for loop with a range, rather than while with a counter if you are iterating over a set number of iterations.

The first comment from @JonSG is also worth noting. If you had set up the individual test as a function, you'd have been forced to have ListOfResults = [] inside the function, so you would have got a new sample of 100 results each time. Something like:

import random as rd

def run_test():
    list_of_results = [rd.randint(0,1) for _ in range(100)]
    number_of_streaks = 0
    for i in range(96):
        if sum(list_of_results[i: i+6]) in(0, 6):
            number_of_streaks += 1
    return number_of_streaks

print([run_test() for _ in range(10)])
print(len(list_of_results))
David Buck
  • 3,752
  • 35
  • 31
  • 35
  • I'm struggling to visualize / conceptualize why my ListOfResuts has the same set of values for each one. Based off the current results, I am flipping the coin 100 times and putting the results in each of the ten slots in ListOfResults. – Trey Nov 15 '21 at 15:37
  • 1
    Because you create the Empty ListOfResults outside the while loop, on the first iteration of the while loop, you add 100 random values to it and then check the first 100 values for streaks. On the next iteration of the while loop, you append 100 new values to ListOfResults making it 200 long, but then iterate over the same first 100 values checking for streaks. Each time round you add 100 new values to the end of the ListOfResults but check the original 100 values, so you get the same result 10 times. – David Buck Nov 15 '21 at 18:31