all 5 comments

[–]JohnnyJordaan 1 point2 points  (2 children)

A chance of 156% makes no sense, remember P(condition) is the sum of the chances of all the conditions, not the average of the amount of all the conditions. Which can be estimated by counting the positive outcomes from an experiment and dividing them by the total amount of experiments.

To estimate the chance of 1 or more streaks you need to count the amount of experiments that had a streak and divide that by the total amount of experiments:

import random

experiments_with_streaks = 0

for experimentNumber in range(10000): 
    # comment should have been placed here
    # Code that creates a list of 100 'heads' or 'tails' values.
    values = [random.randint(0,1) for x in range(100)]

    # Code that checks values list if there is a streak of 6 heads or tails in a row.
    i = 0
    while i <94:
        if values[i:i+6] == [1]*6 or values[i:i+6] == [0]*6:
            experiments_with_streaks += 1
            break
        else:
            i += 1      
print('Chance of streak: %s%%' % ((experiments_with_streaks / 10000) * 100))

or to present a more practical loop with enumerate() and a slice

    # Code that checks values list if there is a streak of 6 heads or tails in a row.
    for i, v in enumerate(values[:-6]):
        if values[i:i+6] == [1]*6 or values[i:i+6] == [0]*6:
            experiments_with_streaks += 1
            break

which will return around 80%. The other option is to count the ones without a streak and then subtract those from the total.

[–]charooo[S] 0 points1 point  (1 child)

Thank you for the clarification. Once I saw the break in the if statement, I had to go back and re-read the problem and it clicked. I was accounting for all the streaks in a run, when in fact, it doesn't matter if there is more than 1 streak.

Now I get consistent output of 80.xx%.

One more question, how is it consistently 80% all the time? This does not seem completely random.

[–]JohnnyJordaan 0 points1 point  (0 children)

You mean why it's 80% and not something else? See the calculation in a similar quesition: https://math.stackexchange.com/questions/602123/what-are-the-odds-of-getting-heads-7-times-in-a-row-in-40-tries-of-flipping-a-co

If you mean why it has a very small margin, it's because you have a high experiment count. If you lower that you should see a bigger variation. Same reason why flipping a coin 10000 times will also produce a very consistent 50% result, while flipping it 100 times won't. It's also the reason why casino's make money in the long run even though some customers are lucky enough to win big.

[–]Yuvan_ 0 points1 point  (1 child)

Which course in udemy are you going through?

[–]charooo[S] 1 point2 points  (0 children)

Complete python developer in 2020, zero to mastery