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.*;
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;
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)
{
account += bet;
bet = 1;
}
else
{
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.