Friday 1 July 2011

The Martingale System of Gambling

Just a small exercise for my edification, regarding the "Gamblers Fallacy" inherent to "The Martingale" system of gambling.

import java.util.*;
import java.io.*;
import java.text.*;

/**
 * A quick implementation of the Martingale
 * system of gambling. Run it, and it will
 * play at the roulette table, until broke.
 * How far can you make it before going
 * broke? Let me know!
 */

public class Martingale
{    

  
  public static String padLeftToTotal(long s, long n) {
    return String.format("%1$#" + (n-(s+"").length()) + 
      "s", s + "");  
  }

  public static void main(String[] stuff)
  {
    long account = 255;
    long bet = 1;
    long maxbet = 0;
    
    // 0..36
    // where red: 1, 3, 5, 7, .. 36
    // where black: 2, 4, 6, .. 35
    // where green : 0
    while (account > 0L)
    {
      long result = 
        Math.round(Math.random()*36.0);
      System.out.println("Account: " + 
        padLeftToTotal(account, 15) + 
        " Betting " + padLeftToTotal(bet, 15) + 
        " $ on red.. result is " + 
        padLeftToTotal(result, 3) + " " + 
        (result == 0 ? "green" : ((result % 2) == 0 ? "black" : "  red")));
      if (result != 0 && (result % 2) == 1)
      {
        // red, we win!
        account += bet;
        bet = 1;
      }
      else
      {
        // not red, we lose!
        account -= bet;
        bet *= 2;
        if (bet > maxbet) 
        {
          maxbet = bet;
          System.out.println("Maximum bet increased to " + maxbet + "$");
          try
          {
            Thread.sleep(1000);
          } catch (Exception e)
          {
          }
        }
      }
    }
    System.out.println("You lose, dude!");
    System.out.println("You now owe the casino " + 
      (-account) + " $");
  }

}

“Remember this well, Lady Luck has no memory.”

References

Wikipedia : Gambler's Fallacy
http://en.wikipedia.org/wiki/Gambler%27s_fallacy
What happened at Monte Carlo in 1913?
http://www.fallacyfiles.org/gamblers.html
Update: 10 February 2015, added Monte Carlo link.


No comments:

Post a Comment