Results 1 to 3 of 3

Thread: gambler.java

  1. #1
    Kragelj is offline Member
    Join Date
    Dec 2010
    Posts
    1
    Rep Power
    0

    Unhappy gambler.java

    /************************************************** ***********************
    * Compilation: javac Gambler.java
    * Execution: java Gambler stake goal N
    *
    * Simulates a gambler who start with 50$ stake and place fair $1 bets
    * until she goes broke or reach 250$ goal. Keeps track of the number of
    * times he wins and the number of bets he makes. Run the experiment N
    * times, averages the results, and prints them out.
    *
    * % java Gambler 50 250 1000
    * Percent of games won = 19.0
    * Avg # bets = 9676.302
    *
    * % java Gambler 50 150 1000
    * Percent of games won = 31.9
    * Avg # bets = 4912.13
    *
    * % java Gambler 50 100 1000
    * Percent of games won = 49.6
    * Avg # bets = 2652.352
    *
    ************************************************** ***********************/
    I need the rest of code in Main class. Anyone can help me??

    public class Gambler {

    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 T = Integer.parseInt(args[2]); // number of trials to perform

    int bets = 0; // total number of bets made
    int wins = 0; // total number of games won

    // repeat N times
    for (int t = 0; t < T; t++) {

    // do one gambler's ruin simulation
    int cash = stake;
    while (cash > 0 && cash < goal) {
    bets++;
    if (Math.random() < 0.5) cash++; // win $1
    else cash--; // lose $1
    }
    if (cash == goal) wins++; // did gambler go achieve desired goal?
    }

    // print results
    System.out.println(wins + " wins of " + T);
    System.out.println("Percent of games won = " + 100.0 * wins / T);
    System.out.println("Avg # bets = " + 1.0 * bets / T);
    }

    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    I need the rest of code in Main class. Anyone can help me??
    No, you're supposed to do your own homework.
    How To Ask Questions The Smart Way

    db

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,656
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Kragelj View Post
    Java Code:
            // repeat N times
            for (int t = 0; t < T; t++) {
    Why do I find this funny?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •