Link Search Menu Expand Document

Martingale and random betting

This section demonstrates how to simulate the Martingale betting strategy using numpy. The Martingale strategy, popular in roulette, follows this structure:

  • 🎲 Start with an initial bet, always betting on black.
  • ⚫ If you win, keep the initial bet.
  • πŸ”΄ If you lose, double the bet.

The essence of this strategy is to double the next bet after a loss to recover the amount lost and obtain a profit. In theory, this sounds promising.

We can implement this strategy as follows. We define an initial amount, a bet, a prob_win probability, and a target amount at which the simulation stops.

import random

def martingale(initial, bet, prob_win, target):
    balance = initial
    bet = bet

    while balance > 0 and balance < initial + target:
        red = random.random() < prob_win
        if red:
            balance -= bet
            bet *= 2
        else:
            balance += bet
            bet = bet

        if bet > balance:
            break

    return balance

The code uses random to simulate the event. The while loop continues betting as long as there is a balance and the target has not been reached. The two scenarios are clearly defined:

  • πŸ”΄ If the result is red, you lose, and the bet is doubled.
  • ⚫ If the result is black, you win, and the bet returns to the initial amount.

Now, we can use our function with specific parameters. We start with an initial balance of 1000 Euros, an initial bet of 10 Euros, a winning probability of 48%, and a target of 2000 Euros. Here is the result:

print(martingale(initial=1000,
                 bet=10,
                 prob_win=0.48,
                 target=2000))

Running the simulation multiple times shows that most of the time, you lose. This is because a streak of bad luck causes the bet to grow exponentially, reaching a point where it can no longer be afforded.

✏️ Exercises:

  • Modify the values to find a strategy that reduces the risk of bankruptcy.