Comparing multiple coin flip experiments. 1 Experiment = 100 Flips. Trying to simulate 10 experiments

Viewed 92
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

1 Answers

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))
Related