A few days ago, my teacher posed me the following problem: you are given two envelopes, and in each of them is a randomly chosen integer, such that all integers have the same probability of being chosen. You open one of the envelopes, and then try to guess if the number you have in your hand is greater or smaller than the number in the second envelope. After you guessed, you can then open the second envelope. You will then repeat this game. Find a strategy such that for each of the following experiments, your chances of winning are greater than 50%.

My strategy is the following: if after $n$ times, (#times the first envelope was greater) > (#times the second envelope was greater), we predict that for the $(n+1)$-th game, the second envelope will be greater and vice-versa.

The reasoning behind this is as follows. For every experiment, assign it the value $1$ if the first envelope contains the larger number and $0$ if the second envelope is larger. After $n$ experiments, the sum of all of them ranges from $0$ to $n$, with the probability $\frac{\binom{n}{k}}{2^n}$ that it is equal to $k$. This means that after the $(n-1)$-th experiment, we must predict in a way such that if our prediction is correct, the sum after the $n$-th experiment will be closer to $n/2$.

So here's the problem: I wrote a program to test my hypothesis, and each time I run it (I repeat the experiment 1 million times), the percentage of correct guesses never passes $50.1$% and it is often below 50%.

Did I make a mistake somewhere, or is there some other problem ?

Here is the python program for those who are interested:

```
import random
counter = 0 #counts the number of correct predictions
score = 0 #counts the number of heads/tails
for i in range(1000000):
if score == i/2:
pred = random.choice((0, 1))
elif score < i/2:
pred = 1
else:
pred = 0
res = random.choice((0, 1))
score += res
if pred == res:
counter += 1
print("counter = " + str(counter))
print("score = " + str(score))
print(str(counter/10000)) #percentage of success
```