naman - 9 months ago 35

Python Question

The expected number of tosses to get two heads in a row is 6. However on executing the below simulation of multiple runs of this experiment, I get a different plot from that expected. Could you help me identify the error in the logic?

Thanks

Code to simulate tosses and check at when two consecutive heads are encountered repeated 10000 times:

`import random`

def toss():

return random.randint(0, 1)

expected_tosses=list()

for test in range(10000):

a=toss()

for i in range(2,100):

b=toss()

if a and b:

expected_tosses.append(i)

break

a=b

import matplotlib.pyplot as plt

plt.hist(expected_tosses, range(1,10))

plt.show()

Answer Source

You are changing the statistics by limiting your upper-loop. Use an infinite loop instead.

The code then looks like the following which should be more precise:

```
import random
def toss():
return random.randint(0, 1)
expected_tosses=list()
for test in range(10000):
a=toss()
i = 2
while True:
b=toss()
if a and b:
expected_tosses.append(i)
break
a=b
i+=1
import matplotlib.pyplot as plt
print(sum(expected_tosses) / float(len(expected_tosses)))
plt.hist(expected_tosses, range(1,10))
plt.show()
```

**The results are okay!**

I introduced a *calculation of the mean* of tosses needed (in the code above) and print it out. You will see, that the means looks like what you expected!

Some example output (we don't use a seed so results will differ each run):

```
5.9941
```

**Just to be clear:** The mean does not tell you that much about the *shape of the histogram*. Maybe this is the source of your confusion (having a near-minimum at 6 in your plot).