Reputation: 974
I am checking out a roulette python scenario. However, I am always winning, I am unable to figure out what I am missing in my algorithm.
The concept is, I start with bet = 1 on even. If I loose, I double the bet, until I win. If I win, I just get back to bet = 1
I wrote a simply python script with randint from (-1 to 36), -1 and 0 for 'double zero' and 'zero'. I ran this simulation in a while loop until 1000 times, almost all the times, I ended up with higher amount than I started. Can I use randint - which is a random number generator, to simulate a roulette?
j = 0
k = 1
while (k<1000): # simulate for 1000 times
bet = 1
bank = 1024
for i in range (1, 100): # Placing 10000 bets
num = random.randint(-1, 36)
if num == 0 or num == -1:
bank = bank - bet
bet = 2*bet
elif(num % 2) == 0:# betting on even numbers
#print("{0} is Even".format(num))
bank = bank + bet
bet = 1
else:
#print("{0} is Odd".format(num))
bank = bank - bet
bet = 2 * bet
if bank<0:
break
if bank<1024:
j = j+1 # Count how many times money is lost
k = k+1
print ('Number of times money lost is', j)
Upvotes: 3
Views: 1260
Reputation: 480
First a couple of syntactical points, some Python-specific:
while (k<1000): # simulate for 1000 times
You're actually simulating 999 times, because k starts counting from 1.
Likewise:
for i in range (1, 100): # Placing 10000 bets
For in in range
actually counts up to the second argument - 1, so your loop counts from 1 to 99, therefore you end up with 99 iterations, not 100.
Then, when it comes parentheses in conditional statements - you don't need them in Python, or at least not in your cases, unlike in most other programming languages I know.
while (k<1000):
elif(num % 2) == 0:
can be changed to:
while k<1000:
and elif num % 2 == 0:
Next up, in your code you have this:
bet = 2*bet
and j = j+1
and so on.
Well, you can just write
bet *= 2
and j += 1
instead.
This syntax is supported by many languages, and in addition many allow you to write j++
or j--
if you're incrementing or decrementing by 1, respectively, but Python doesn't support that.
I hope this helps. Now as for your question, I believe the issue is this. I think the odds actually aren't even, not even close. They're skewed towards winning. If you wanted to have even odds, you'd write something like this:
if num % 2 == 0:
bank += bet
bet *= 2
else:
bank += bet
bet *= 2
Meaning, you'd double the bet regardless of whether the previous coin flip was a loss or a win. If you do that, you end up with catastrophic results.
Let me note my knowledge of stochastics is very limited, but the issue seems quite clear to me. Because the odds are close to 50%, you're statistically more likely to flip the opposite of your previous result. Since you reset the bet after a win, the loss that comes next will be smaller. You then double the bet and receive double the amount of money. If you reverse the logic and reset the bet after a loss, you win less and lose more and end up once again, with catastrophic results, given this sample size (in the nested loop), losing 999 times out of 999.
Now, if given the original formula, you increase the number of bets per "round" to 100 thousand (from the 99 in your nested loop), suddenly you end up losing close to 999 times out of 999 as well. Why is that? Well, I believe that since the strategy relies on flipping the opposite side of the coin every time, the more samples, the greater the chance of that not being the case (and this would be the "losing streak" the Wikipedia entry Frank posted talks about).
Upvotes: 1