I tried to code a simple python program which does the following: it generates random numbers between 0 and 5 until a zero is generated twice in a row. it does this N times and calculates the average number of generated numbers needed to get two zeroes in a row.
The second part of the program does exactly the same, except it stops when 1 is generated after a 0.
I expected the average number of repetitions needed in both cases would be about the same, but there is a notable difference. I cannot find an error in my code, nor in my intuition. But there is certainly an error in one (or both).
I tried to simplify my code as much as I could:
import math
import random as rnd
def null_null():
ct=0
first=(math.floor(rnd.random()*6))
second=(math.floor(rnd.random()*6))
while (first!=0 or second!=0):
first=second
second=(math.floor(rnd.random()*6))
ct+=1
return ct
def null_one():
ct=0
first=(math.floor(rnd.random()*6))
second=(math.floor(rnd.random()*6))
while (first!=0 or second!=1):
first=second
second=(math.floor(rnd.random()*6))
ct+=1
return ct
res1=res2=0
N=1000
for i in range(N):
res1+=null_null()
res2+=null_one()
print("Average for 0-0: ", res1/N)
print("Average for 0-1: ", res2/N)
Preliminary
The probability to get a $0$ is $\frac{1}{6}$. Therefore, the expected number of digits to get a $0$ is $6$.
Consecutive 0s
Let the expected sequence length to get the first consecutive $0$s be $x$. The expected length must satisfy the recursive relation:
$$ x = 6+\frac{1}{6}\cdot 1 + \frac{5}{6}\cdot \left(x+1\right) $$
Some explanations:
Solving for $x$, we get $x=42$. In your script, you have two digits before the loop starts. Which is why you get on average $40=42-2$ iterations.
0 followed by 1
Let the expected sequence length to get the first $01$ be $x$. Let the expected additional sequence length to get the first $01$ when our last digit is $0$ be $y$. The two must satisfy the relation:
$$ \begin{aligned} x &= 6 + y \\ x &=6+\frac{1}{6}\cdot 1 + \frac{4}{6}\cdot \left(x+1\right) + \frac{1}{6}\cdot\left(y+1\right) \end{aligned} $$
Some explanations:
Solving for $x$, we get $x=36$. Again, your loop start with two digits, so you get on average $34=36-2$ iterations.