// This program simulates making bets at a Roulette table. // It assumes a European roulette table (no 00, which hurts the players odds) import java.util.*; public class Roulette { public static int WHEEL_SIZE = 37; // 0-36 public static int RED_MIN = 1; public static int RED_MAX = 18; public static int INITIAL_MONEY = 500; public static int NUMBER_TRIALS = 10000; // better would be more trials for higher bets public static void main(String[] args) { Random r = new Random(); System.out.println("Go for broke:"); for(int i=10; i <= 500; i += 10) { simulateBetUntilBroke(r,i); } System.out.println("Try to double your money:"); for(int i=10; i <= 500; i += 10) { simulateTryToDouble(r,i); } System.out.println("Try to exactly double your money:"); for(int i=10; i <= 500; i += 10) { simulateTryToExactlyDouble(r,i); } } // take current amount of money and a bet and return new amount of money public static int makeOneRedBet(Random r, int startMoney, int bet) { if(startMoney < bet) { // can't bet more than you have bet = startMoney; } int spin = r.nextInt(WHEEL_SIZE); // System.out.println("spin is " + spin); if(spin >= RED_MIN && spin <= RED_MAX) { return startMoney + bet; } else { return startMoney - bet; } } public static void simulateBetUntilBroke(Random r, int bet) { int spinsSum = 0; for(int i=1; i <= NUMBER_TRIALS; ++i) { int currentMoney = INITIAL_MONEY; int spins = 0; while(currentMoney > 0) { ++spins; // System.out.println("I have $" + currentMoney); currentMoney = makeOneRedBet(r, currentMoney, bet); } // System.out.println("I got to play " + count + " times"); spinsSum += spins; } double avg = (double)spinsSum / NUMBER_TRIALS; System.out.printf("On average, survived %.1f spins when the bet was %d\n", avg, bet); } public static void simulateTryToDouble(Random r, int bet) { int spinsSum = 0; int numWins = 0; for(int i=1; i <= NUMBER_TRIALS; ++i) { int currentMoney = INITIAL_MONEY; int spins = 0; while(currentMoney > 0 && currentMoney < 2*INITIAL_MONEY) { ++spins; // System.out.println("I have $" + currentMoney); currentMoney = makeOneRedBet(r, currentMoney, bet); } // System.out.println("I got to play " + count + " times"); spinsSum += spins; if(currentMoney > 0) { ++numWins; } } double avg = (double)spinsSum / NUMBER_TRIALS; double winPct = 100.0 * numWins / NUMBER_TRIALS; System.out.printf("On average, survived %.1f spins and won %.1f%% when the bet was %d\n", avg, winPct, bet); } public static void simulateTryToExactlyDouble(Random r, int bet) { int spinsSum = 0; int numWins = 0; for(int i=1; i <= NUMBER_TRIALS; ++i) { int currentMoney = INITIAL_MONEY; int spins = 0; while(currentMoney > 0 && currentMoney < 2*INITIAL_MONEY) { ++spins; // System.out.println("I have $" + currentMoney); if(currentMoney + bet <= 2 * INITIAL_MONEY) { currentMoney = makeOneRedBet(r, currentMoney, bet); } else { currentMoney = makeOneRedBet(r, currentMoney, 2*INITIAL_MONEY - currentMoney); } } // System.out.println("I got to play " + count + " times"); spinsSum += spins; if(currentMoney > 0) { ++numWins; } } double avg = (double)spinsSum / NUMBER_TRIALS; double winPct = 100.0 * numWins / NUMBER_TRIALS; System.out.printf("On average, survived %.1f spins and won %.1f%% when the bet was %d\n", avg, winPct, bet); } }