# Simulate a doubling betting strategy in C#

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.

If you bet black and the ball lands on black, you win. 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. Google “roulette” for more details.

Because you lose if the ball lands red or green (assuming you bet black), the chance of you winning is 18/37, which is slightly less than 1/2. That means your expected value from betting \$1 is 18/37 * \$2 ≈ \$0.973. 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 fron 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 the same 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. Then 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 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 black 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 this round, the wheel would need to come up black six times in a row. The odds of that happening are roughly (1/2)^6 = 1/256. That seems like it won’t 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.

# Using the Program

To use the program, enter your starting amount of money in the Current Bank text box. Enter the total amount that you would like to win in the Stop At box. Then click Go.

As it simulates the betting strategy, the program displays your maximum bank, the number of rounds it has played, the current bet, whether you won or lost the previous round, and your largest bet.

The program simulates the betting strategy until your bank reaches the stopping amount or you no longer have enough money to cover your next bet. (Another condition that you might include would be a maximum number of rounds. You probably can’t run 10,000 rounds at a real roulette wheel before you collapse from exhaustion.)

If you pick specific values, for example a \$100 initial bank and a stopping amount of \$150, you’ll win some times and lose others. If you perform many trials and keep track, you’ll find that you lose overall.

If you enter very large values, you can test the program to see what happens if you have an almost unlimited amount of money. You can play for quite a long time, but eventually you’ll lose.

# The Code

When you click the Go button, the program executes the following code.

```private int Round;
private decimal MaxBet, CurrentBank, MaxBank, LastBet;
private bool LastWon;
private Random Rand = new Random();
private int StopAt = 0;

private void btnGo_Click(object sender, EventArgs e)
{
if (tmrBet.Enabled)
StopSimulation();
else
StartSimulation();
}

private void StopSimulation()
{
tmrBet.Enabled = false;
tmrBet.Tag = "Go";
}

private void StartSimulation()
{
tmrBet.Tag = "Stop";
Round = 0;
MaxBet = 0;
StopAt = int.Parse(txtStopAt.Text);
CurrentBank = decimal.Parse(txtCurrentBank.Text);
MaxBank = CurrentBank;
LastBet = 0;
LastWon = true;
tmrBet.Enabled = true;
}```

This code begins with variables to track various values. The button’s Click event handler checks the program’s tmrBet timer. If the timer is enabled, then the simulation is currently running. In that case the code calls the StopSimulation method.

If the timer is disabled, then you are trying to start a new run so the code calls the SgtartSimulation method.

The StopSimulation method disables the timer and sets the button’s caption to Go so it is ready to start a new run.

The StartSimulation method sets the button’s caption to Stop, resets the program’s counters, and enables the timer.

The following code shows the timer’s Tick event handler taht executes when the timer fires.

```private void tmrBet_Tick(object sender, EventArgs e)
{
decimal current_bet;
if (LastWon) current_bet = 1;
else current_bet = LastBet * 2;

Round++;
txtRound.Text = Round.ToString();

txtCurrentBet.Text = current_bet.ToString();

if (StopAt < CurrentBank)
{
StopSimulation();
return;
}

if (current_bet > CurrentBank)
{
StopSimulation();
return;
}

LastBet = current_bet;
if (MaxBet < current_bet)
{
MaxBet = current_bet;
txtMaxBet.Text = MaxBet.ToString();
}

int spin = Rand.Next(37);
LastWon = ((spin != 0) && (spin % 2 == 0));
if (LastWon)
{
CurrentBank += current_bet;
txtWinLoss.Text = "WIN";
}
else
{
CurrentBank -= current_bet;
txtWinLoss.Text = "LOSS";
}

txtCurrentBank.Text = CurrentBank.ToString();
if (MaxBank < CurrentBank)
{
MaxBank = CurrentBank;
txtMaxBank.Text = MaxBank.ToString();
}

if (CurrentBank < 1) StopSimulation();
tmrBet.Enabled = false;
}```

This code simulates the betting strategy. It’s fairly straightforward.

The code first checks LastWon to see if the previous bet was won. If the last bet was one, the code sets the next bet to \$1. If the previous bet was lost, then the code sets the next bet to twice the previous bet.

Next the code increments and displays the round number, and displays the current bet. It then checks the current bank for stopping conditions. If the bank has reached the desired stopping amount, the code calls the StopSimulation method and returns. If the bank is smaller than the next required bet, the code also calls the StopSimulation method and returns. The code saves the current bet in variable LastBet and updates MaxBet if necessary.

Next the program picks a random number between 0 and 36. If the number is greater than 0 (the green number) and even (the black numbers are even), then we have won and the program adds the current bet to the bank. If the number is 0 (green) or odd (red), the code subtracts the current bet from the bank.

The event handler updates MaxBank if appropriate. Finally, if the current bank is less than 1, the code calls the StopSimulation method.

# Conclusion

The example demonstrates a couple of useful techniques such as using a timer to run a simulation and using the same button to start and stop the simulation.

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.

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, trying modifying this example so it simulates that approach and see what happens.

## About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in algorithms, mathematics and tagged , , , , , , , , , . Bookmark the permalink.

### 1 Response to Simulate a doubling betting strategy in C#

1. Trevor Loughlin says:

This is a useful demonstration of the futility of gambling unless you use a hardware quantum random number generator and some software with very clever maths to overcome the “no communication theorem” and actually get your program to send the results back in time to itself. Even then the temptation is to get greedy and force the device to bet more than is safe. But your graphics programs can be modified to make a neat casino bot that eventually will auto-calibrate on any online game. This takes out the human point of failure in the loop! But the stock market and predictions of terrorist attacks and serial killings is my real aim.
Incidentally after each prediction you reset the device to see if the altered timeline has any new nasty surprises in store until the ideal path in the multiverse is achieved (though in the case of Dr Strange versus Thanos it was more about the least worse option.) So when I am finished translating everything from the easy joy of VB6 with a bit of windows API (where I have a working program but no web access on my old Vista machine) to the pure hell of c# I will set up a client for you to see the future because you have saved me some time.
One thing I am working out is how to hijack a desktop magnifier in C# to capture the results. I have modified the program that captures a mouse selected area to always go to a fixed area to get the numbers, but this is from a file of a full screen capture whereas I want to capture a specific area (the result) directly and OCR this to the predictor. But I have learnt quite a bit from you so I should find the handles to replace the manually selected rectangle with hard code.

Comments are closed.