Martingale

martingale <- function(initial.bet, starting.amount, no.days) {

  iter <- c(1:(60*8))
  rouletteWheel <- c(1:38)
  days <- 1:no.days
  totals <- data.frame(days)
  totals$finalProfit <- NA
  totals$maxLoss <- NA

  ## number of days at the casino
  for (j in 1:length(days)){

    ## pre-allocate variables
    df <- data.frame(iter)
    df[,'wheel'] <- sample(rouletteWheel,length(iter),replace=TRUE)
    df[,'win1'] <- NA

    ## assign inital bet amount
    df$bet1 <- initial.bet

    df$payout1 <- NA
    df[,'cumul'] <- 0
    df$cumul[1] <- starting.amount

    ## cases where you win on 19-36 result
    df[df$wheel < 19,'win1'] <- 'l'
    df[df$wheel > 36, 'win1'] <- 'l'
    df[df$wheel > 18 & df$wheel < 37,'win1'] <- 'w'

    ## no of roulette iterations at the casino
    for (i in iter){

      if (i != 1){
        ## if previous case was a loss, double the bet (otherwise value is the initial.bet value)
        if (df$win1[i-1] == 'l'){
          df$bet1[i] <- df$bet1[i-1]*2
        }
      }

      ## if the case was a win, the payout is the bet
      if (df$win1[i] == 'w'){
        df$payout1[i] <- df$bet1[i]*1
      } else {
        df$payout1[i] <- df$bet1[i]*-1
      }
      if (i!= 1){
        df$cumul[i] <- df$payout1[i] + df$cumul[i-1]
      } else {
        df$cumul <- starting.amount
      }
    }

    totals$finalProfit[j] <- df$cumul[i-1]
    totals$maxLoss[j] <- max(df$bet1)*-1
    totals$minAccAmt[j] <- min(df$cumul)
  }
  return(list(totals = totals, last_day = df))
}

num_days <- 10
initial_bet <- 10 # initial betting amount used each time
starting_amount <- 100 # start with $100 and see if this will last without going negative

results <- martingale(initial_bet, starting_amount, num_days)

## final daily profits
results$totals$finalProfit

## the last day
results$last_day[1:10,]
results$last_day[1:100,]

## totals by day
results$totals