Another well-known gambling system is the martingale doubling system. Suppose that you are betting on red to turn up in roulette. Every time you win, bet 1 dollar next time. Every time you lose, double your previous bet. Suppose that you use this system until you have won at least 5 dollars or you have lost more than 100 dollars. Write a program to simulate this and play it a number of times and see how you do. In his book The Newcomes, W. M. Thackeray remarks “You have not played as yet? Do not do so; above all avoid a martingale if you do.” Was this good advice?


I tried the martingale system 5, 20, 35, and 50 times, and ended up with very variable (mostly positive) winnings. I may have set up my function incorrectly, but it seems like it works well with 1:1 odds. I rarely ended up with negative winnings.


martingale <- function(bet, n) {
  red <- c(1, 27, 25, 12, 19, 18, 21, 16, 23, 14, 9, 30, 7, 32, 5, 34, 3, 36, 1)
  black <- c(10, 29, 8, 31, 6, 33, 4, 35, 2, 28, 26, 11, 20, 17, 22, 15, 24, 13)
  green <- c(00, 0)
  
  roulette <- c(red, black, green)
  
  spin <- sample(roulette, n, replace = TRUE)
  
  winnings <- 0
  
  for (i in 1:length(spin)) {
    if (spin[i] %in% red) {
      winnings <- winnings + (bet * 2)
      bet <- 1
  
  } else {
    winnings <- winnings - bet
    bet <- bet * 2
  }
  }

  print(sprintf("At the end of %d games, I have bet $%d and have $%d in the bank", n, bet, winnings))
}

martingale(1, 5)
## [1] "At the end of 5 games, I have bet $32 and have $-31 in the bank"
martingale(1, 20)
## [1] "At the end of 20 games, I have bet $2 and have $93 in the bank"
martingale(1, 35)
## [1] "At the end of 35 games, I have bet $8 and have $62 in the bank"
martingale(1, 50)
## [1] "At the end of 50 games, I have bet $1 and have $114 in the bank"