001package ruin; 002 003import java.util.Scanner; 004 005/** 006 * This is our first simulation. 007 * 008 * <a href="http://mathworld.wolfram.com/GamblersRuin.html">See here</a> 009 * to learn more about the math. 010 * 011 * @author Leon Tabak 012 * @version 11 February 2015 013 */ 014public class Ruin { 015 016 /** 017 * All of my code is in one method. 018 * 019 * @param args are the command line arguments---we do not use these. 020 */ 021 public static void main(String[] args) { 022 Scanner scanner = new Scanner(System.in); 023 024 System.out.print("Enter the amount of money that you " 025 + "will take to the casino: "); 026 int stake = scanner.nextInt(); 027 028 System.out.print("How much money do you want to win " 029 + "before you leave the casino: "); 030 int goal = scanner.nextInt(); 031 032 System.out.print("How many times do you want to run the experiment: "); 033 int numberOfTrials = scanner.nextInt(); 034 035 for (int i = 0; i < numberOfTrials; i++) { 036 int numberOfBets = 0; 037 int balance = stake; 038 while (balance > 0 && balance < goal) { 039 boolean win; 040 if (Math.random() < 0.5) { 041 win = true; 042 } // if 043 else { 044 win = false; 045 } // else 046 047 if (win == true) { 048 balance = balance + 1; 049 } // if 050 else { 051 balance = balance - 1; 052 } // else 053 054 numberOfBets = numberOfBets + 1; 055 } // while 056 057 System.out.println("number of bets = " + numberOfBets); 058 System.out.println("balance = " + balance); 059 } // for 060 061 } // main( String [] ) 062 063} // Ruin