TITLE: When should I stop playing this dice game?
QUESTION [5 upvotes]: The rules are as follows:
You start with \$1 and roll a six-sided die. If you roll anything but a 1, you double your money (so \$2 for the first roll, $4 for the second, and so on). If you roll a 1, you lose all your money. What is the optimal number of times you should roll the die to make the most money?
My initial theory:
The probability of rolling 2-6 consecutively $n$ times is $(\frac{5}{6})^n$; and the probability of rolling a 1 is just $\frac{1}{6}$. So, the expected value is going to be:
$$
E = (\frac{5}{6})^n2^n-\frac{1}{6}2^n \\
=2^n(\frac{5}{6}^n-\frac{1}{6})
$$
Now, if I solve for $n_{L}$ where $E=0$:
$$
0=2^{n_{L}}(\frac{5}{6}^{n_{L}}-\frac{1}{6}) \\
\frac{1}{6}=\frac{5}{6}^{n_{L}} \\
n_{L}=log_{5/6}\frac{1}{6}\\
\approx9.8
$$
Which means I should roll around 9 times to maximize my profit (or maybe 10 if I'm feeling lucky).
However, I tried running a quick simulation and got $n_{L}=5.01 \pm 0.05$ after 10,000 games.
Where did I go wrong? Thank you!
For reference:

import random
import numpy as np

def dice():                                                                    
    return random.randint(1,6)
 
ns=np.array([])
for i in range(10000):
    n=0 
    while dice() != 1:
        n+=1
    ns=np.append(ns,[n])
print(np.average(ns))
print(np.std(ns)/100)

Output

5.0093
0.05511008393207182

Edit:
Here's a more accurate simulation that reflects Ross Millikan's answer for my own reference.
import random
import numpy as np
                                                                               
def dice():
    return random.randint(1,6)
    
trials=np.array([[]])
for i in range(30):
    moneys=np.array([])
    for j in range(100):
        money=1
        numrolls=0
        while numrolls <= i:
            numrolls+=1
            if dice() != 1:
                money=2*money
            else:
                money=0
                break
        moneys = np.append(moneys,[money])
    print("i: "+str(i))
    trial = np.array([i,np.average(moneys)])
    print("money: "+str(np.average(moneys)))
    trials = np.append(trials,trial)            

Output
i: 0
money: 1.74
i: 1
money: 2.48
i: 2
money: 4.88
i: 3
money: 6.56
i: 4
money: 14.08
i: 5
money: 28.8
i: 6
money: 32.0
i: 7
money: 40.96
i: 8
money: 81.92
i: 9
money: 112.64
i: 10
money: 245.76
i: 11
money: 737.28
i: 12
money: 1146.88
i: 13
money: 327.68
i: 14
money: 2293.76
i: 15
money: 3276.8
i: 16
money: 6553.6
i: 17
money: 13107.2
i: 18
money: 26214.4
i: 19
money: 52428.8
i: 20
money: 20971.52
i: 21
money: 0.0
i: 22
money: 83886.08
i: 23
money: 335544.32
i: 24
money: 335544.32
i: 25
money: 671088.64
i: 26
money: 1342177.28
i: 27
money: 8053063.68
i: 28
money: 0.0
i: 29
money: 0.0

REPLY [2 votes]: Your code is calculating the average number of rolls where you successfully cash out but it doesn't account for the fact that the prize doubles at each step. The expected value does and so taking more risks for the much larger reward is optimal at each step.