public class GamblersRuin {

  public static final int MAXINT = Integer.MAX_VALUE;  // largest integer

  public static void main(String[] args) {
      
    int stake  = Integer.parseInt(args[0]);  // gambler's stating bankroll
    int goal   = Integer.parseInt(args[1]);  // gambler's desired bankroll
    int trials = Integer.parseInt(args[2]);  // number of trials to perform

    int wins = 0;        // number of wins
    int total_bets = 0;  // total number of bets made
    int max_bets = 0;    
    int min_bets = MAXINT;
    
    // repeat trials times
    for (int t = 0; t < trials; t++) {
      // do one gambler's ruin simulation
      int bets = 0;
      int cash = stake;
      while (cash > 0 && cash < goal) {
        bets++;
        if (Math.random() < 0.5) cash++; // win $1
        else cash--;                     // lose $1
      }
      total_bets += bets;
      if (cash == goal) wins++;
    }

    // print results
    System.out.println("It resulted in " + wins + " wins out of " + trials
      + " trials.");
    System.out.println("The percentage of wins: " + 100.0*wins/trials
      + "% ");
    System.out.println("The average number of bets, win or lose: "
      + 1.0*(total_bets)/trials);
    
  }

}
