[Rod Stephens Books]
Index Books Python Examples About Rod Contact
[Mastodon] [Bluesky]
[Build Your Own Ray Tracer With Python]

[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[Book: Interview Puzzles Dissected] This example solves a puzzle that is vaguely reminiscent of my book Interview Puzzles Dissected. That book explains how to solve more than 200 interesting puzzles that have been used in technical interviews in the past. (Although thankfully that sort of thing is no longer the norm.)

Title: Simulate a bet doubling strategy in Python

[This program simulates a bet doubling strategy in Python.]

Casinos love customers who have a betting strategy because those customers always end up losing. As long as the game's events are independent of each other (for example, one roll of the dice does not depend on the previous rolls), the odds favor the house, so no strategy will win in the long run.

One well-known betting strategy doubles your bet every time you lose. This example simulates that strategy so you can get an idea of why it doesn't work.

The Strategy

This example uses roulette, but any game that pays 1-to-1 will work. A European/French style roulette wheel has 37 slots numbered 0 through 36. Number 0 is green. Of the numbers 1 through 36, half are red and half are black. American style wheels have an additional green value, 00. This program simulates the American version.

If you bet black and the ball lands on black, you win your bet plus that same amount again. For example, if you bet $1, you get back $2.

If you bet black and the ball lands on red or green, you lose. Roulette is a pretty complicated game, but for this example that's about all you need to know. Search online for more details.

Because you lose if the ball lands on red or green (assuming you bet black), the chance of you winning is 18/38, which is slightly less than 1/2. That means your expected value from betting $1 is 18/38 * $2 ≈ $0.947. That should be your tip off right there. Because a $1.00 bet earns you only $0.973, you're going to lose in the long run.

Note that it doesn't matter which color you bet on. You can bet on red if you like or even switch colors from bet to bet, either in a pattern or randomly. (Just stay away from green because the odds aren't close to 1-to-1. The result is about the same because the payout is larger, but the analysis is harder.)

Okay, so here's the betting strategy. You start by betting $1 on black. If you lose, you double your bet and place $2 on black. If you lose again, you bet $4 on black. You keep doubling your bet until you win.

Suppose your last bet was $N. After this process, you have bet 1 + 2 + 4 + 8 + ... + N = 2 * N - 1. You keep your final bet plus you win $N, so you end up with $2 * N, a profit of $1.

Now you start the whole thing over again by betting $1.

The betting strategy's logic is that you will eventually win, so you always win $1. Simply repeat until you're rich!

The Catch

That strategy seems reasonable and works except for one small but important detail: you don't have an infinite amount of money. That means eventually the wheel will produce enough red spins in a row to wipe you out.

For example, suppose you start with $63. Then you can afford to bet $1, $2, $4, $8, $16, and $32 before you run out of money. That means to lose in your first round of the strategy, the wheel would need to come up red six times in a row. The odds of that happening are roughly (1/2)6 = 1/256. That seems like it would never happen, so you think you're safe.

In fact, you probably will win this first round. Unfortunately you won't stop there. You'll use the betting strategy again and again until you eventually lose. You may win for a while, but eventually the odds will catch up to you.

The Code

The double_bets method takes as parameters your initial bet (typically $1), the amount of money you have in the bank, and the maximum number of times you want to place a bet. The method simulates a roulette game and prints out your bank and current roll at each step.

The following code shows the method.

import random def double_bets(initial_bet, bank, max_rounds): '''Simulate a roulette wheel. It has 38 slots numbered 0 to 37 plus 00.''' max_bank = bank current_bet = initial_bet for i in range(max_rounds): # Value -1 represents 00. # Player wins on even number greater than 0. spin = random.randint(-1, 37) is_winner = (spin > 0) and (spin % 2 == 0) if is_winner: # We won!😊 Pocket the winnings and start over. bank += current_bet max_bank = max(max_bank, bank) current_bet = initial_bet else: # We lost.😔 Double the bet if we have that much money left. bank -= current_bet current_bet = min(2 * current_bet, bank) # Display our progress. print(f'{i+1:<3}: Bank: ${bank}, bet: ${current_bet}') if bank < 1: break # Bail if we're out of money. # Print a summary. print() print(f'Max Bank: ${max_bank}') print(f'Final Bank: ${bank}')

This code enters a loop where it performs the bets. Each time it picks a random number between -1 and 36 inclusive. The values 0 and -1 represent 0 and 00 respectively, both of which mean you lost. If the number is greater than 0 and even, then you win.

If you win, the program adds the current bet to your bank and resets the current bet to the initial bet value.

If you lose, the program subtracts the current bet from your bank and then doubles the current bet if you have that much money left.

The loop continues until you've reached your maximum number of bets or you run out of money, at which point the code displays the maximum amount of money you ever had and your final bank balance.

Here's the test code.

double_bets(1, 100, 100)

This code calls the double_bets method with the initial bet $1, a $100 bank, and simulating up to 100 rounds of betting. If you run the program a bunch of times, you will sometimes reach 100 bets and may finish slightly ahead. Usually, however, you'll go broke before 100 bets.

Conclusion

Download the example and experiment with it. It often produces some winnings before eventually losing everything. If only you could know when to stop, you could come out ahead. But if you knew when to stop, you could dominate the stock market, too.

While this is not a good financial strategy, it can be decent entertainment. If you start with $100, you may be able to play for an hour or two before you run out of money. And there's a small chance that you'll come out ahead.

Before I leave this alone, I'll mention one other betting strategy that is even worse than the doubling strategy. The odds of the ball landing red five times in a row are roughly (1/2)5 = 1/32. The strategy is to wait until the ball has landed on red four times in a row and then bet on black. The reasoning is that it is unlikely to land on red again.

This is probably the most common mistake people make in statistical reasoning. Because every spin of the wheel is independent of the previous spins, there is a roughly 1/2 chance that the next spin will be black, not 1/32. That should be obvious, but I've talked with people that defended this strategy with great enthusiasm. These days there are a lot of people who believe far weirder things like the Earth is flat, so this shouldn't be a huge surprise. If you don't see why the strategy won't work, try modifying this example so it simulates that approach and see what happens.

Download the example to see all of the details.

© 2024 Rocky Mountain Computer Consulting, Inc. All rights reserved.