Dice Frequency Simulation

Discussion in 'Java' started by cb17890@yahoo.com, Oct 13, 2007.

  1. Guest

    Hi I'm new to JAVA Programming and need help with my homework!

    The assignment: Design and implement a simple, well-documented JAVA
    program that uses random numbers to simulate throwing a pair of dice
    and recording the total score thrown. That is, each roll will throw
    two dice, each of which can randomly result in a face up value of 1,
    2, 3, 4, 5, or 6. The program should add the two values to get a
    score for the roll. The score will be between 2(1+1) and 12(6+6).
    The program should loop to roll the dice a total of 500 times and
    should sum up how many times each score was thrown. That is, how many
    of the 500 times was a total of 2 thrown, how many times was a total
    of 3 thrown, etc. You will need one accumulator for each of the 11
    possible scores. Afer all 500 rolls have been thrown and counted, the
    program should report for each possible score (2-12), how many times
    it was thrown and what % of the time it was thrown. This program
    will have no input. It should produce output similar to the
    following:

    Total Number Percent
    Rolled of Times of Time

    So far, I know to use the for loop with the following values: (count =
    2; count <= 500; count++) but then I'm stuck. I don't know what to do
    next!!
     
    , Oct 13, 2007
    #1
    1. Advertising

  2. wrote:
    > Hi I'm new to JAVA Programming and need help with my homework!
    >
    > The assignment: Design and implement a simple, well-documented JAVA
    > program that uses random numbers to simulate throwing a pair of dice
    > and recording the total score thrown. That is, each roll will throw
    > two dice, each of which can randomly result in a face up value of 1,
    > 2, 3, 4, 5, or 6. The program should add the two values to get a
    > score for the roll. The score will be between 2(1+1) and 12(6+6).
    > The program should loop to roll the dice a total of 500 times and
    > should sum up how many times each score was thrown. That is, how many
    > of the 500 times was a total of 2 thrown, how many times was a total
    > of 3 thrown, etc. You will need one accumulator for each of the 11
    > possible scores. Afer all 500 rolls have been thrown and counted, the
    > program should report for each possible score (2-12), how many times
    > it was thrown and what % of the time it was thrown. This program
    > will have no input. It should produce output similar to the
    > following:
    >
    > Total Number Percent
    > Rolled of Times of Time
    >
    > So far, I know to use the for loop with the following values: (count =
    > 2; count <= 500; count++) but then I'm stuck. I don't know what to do
    > next!!
    >


    For general advice on getting started, see
    http://home.earthlink.net/~patricia_shanahan/beginner.html

    In particular, I suggest reducing the size of the problem and working
    through doing it paper and pencil.

    java.util.Random has a method nextInt(int) which is useful for dice rolls.

    Patricia
     
    Patricia Shanahan, Oct 13, 2007
    #2
    1. Advertising

  3. On Fri, 12 Oct 2007 21:58:30 -0700, wrote:
    > So far, I know to use the for loop with the following values: (count
    > = 2; count <= 500; count++) but then I'm stuck. I don't know what to
    > do next!!


    I won't do your homework for you, but wonder how you came up with
    those particular loop values. How many iterations are you expecting?

    You might start by writing a program that just rolls one die once.

    /gordon

    --
     
    Gordon Beaton, Oct 13, 2007
    #3
  4. Roedy Green Guest

    On Fri, 12 Oct 2007 21:58:30 -0700, wrote, quoted or
    indirectly quoted someone who said :

    >Hi I'm new to JAVA Programming and need help with my homework!


    see http://mindprod.com/jgloss/homework.html

    Just posting your assignment and hoping others will do it for you
    won't fly very far, partly because you would learn nothing, subverting
    your enormous investment in time and money to have a school teach you
    Java.

    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Oct 13, 2007
    #4
  5. Roedy Green Guest

    Roedy Green, Oct 13, 2007
    #5
  6. rossum Guest

    On Fri, 12 Oct 2007 21:58:30 -0700, wrote:

    >Hi I'm new to JAVA Programming and need help with my homework!
    >
    >The assignment: Design and implement a simple, well-documented JAVA
    >program

    Can you write comments? "Well documented" means putting in comments.
    If you have covered documentation comments /** ... */ then use those
    as well.


    >that uses random numbers to simulate throwing a pair of dice
    >and recording the total score thrown.

    Look up java.util.Random in the Java documentation.

    >That is, each roll will throw
    >two dice, each of which can randomly result in a face up value of 1,
    >2, 3, 4, 5, or 6.

    Stop here and write a program to simulate rolling a single six-sided
    die and print out the result. Test your program and make sure it
    works correctly, no rolls of 0, no rolls of 7, no rolls of -3629533.
    When it works correctly you can proceed. Do not proceed until you
    have go it working right.

    You will build up your homework by adding to this initial program so
    you need to be sure it works correctly now. It is easier to find the
    errors in a small program than in a large one. If you get problems
    then post your code here and we will help you.

    >The program should add the two values to get a
    >score for the roll.
    >The score will be between 2(1+1) and 12(6+6).

    Now modify your program to roll two dice and add the two values to get
    a total score. Again test and make sure that the total score is
    always in the range [2, 12], no 1, no 13.

    >The program should loop to roll the dice a total of 500 times

    Modify your program to roll the pair of dice 500 times. Test and make
    sure it is right before you carry on.

    >and
    >should sum up how many times each score was thrown. That is, how many
    >of the 500 times was a total of 2 thrown, how many times was a total
    >of 3 thrown, etc. You will need one accumulator for each of the 11
    >possible scores.

    You will need to think of a way to keep the running counts of the
    different scores. Think about which parts of Java you have learned in
    class that would be useful to keep a lot of numbers like this. Add
    this group of accumulators to your program so that it keeps the
    running counts for the 500 die rolls. Make sure that you are
    documenting everything. Test again.

    >Afer all 500 rolls have been thrown and counted, the
    >program should report for each possible score (2-12), how many times
    >it was thrown and what % of the time it was thrown. This program
    >will have no input. It should produce output similar to the
    >following:
    >
    >Total Number Percent
    >Rolled of Times of Time

    Add code to display the specified headings into your program. Add
    more code to output the total rolled (2, 3, ... 12) and the number of
    times each total was rolled. Test.

    Add code to calculate the percentage for each of the different totals
    rolled and to display the percentage. Check that your program is
    "well-documented". Read through the question again to make sure that
    you have not left anything out. Test your program again for the last
    time.

    Congratulations! You have now completed your homework.

    The usual method to tackle a large difficult problem is to start with
    a small easy problem related to the large one - in this case we
    started by rolling a single die. By building on the small easy
    problem we can work up to the large difficult problem in small stages,
    testing all the way. Testing is important, as you probably noticed.
    If there is a bug in an early stage it will still be there in the
    later stages and will be more difficult to find.

    >So far, I know to use the for loop with the following values: (count =
    >2; count <= 500; count++) but then I'm stuck. I don't know what to do
    >next!!

    Your code so far has errors in it - remember the importance of testing
    :). As I said, don't start with 500 double rolls, start with a single
    roll of one die. Work up to 500 double rolls in steps.

    rossum
     
    rossum, Oct 13, 2007
    #6
  7. rossum wrote:
    >>Hi I'm new to JAVA Programming and need help with my homework!
    >>
    >>The assignment: Design and implement a simple, well-documented JAVA
    >>program

    >Can you write comments? ...

    (snip long, detailed post)

    Wow! Except for being more specific to the OP's problem,
    that post sounded strongly reminiscent of the advice Patricia
    sets out in her 'beginner' document, ..perhaps combined with
    the doc. on debugging*.

    * <http://home.earthlink.net/~patricia_shanahan/debug/index.html>

    --
    Andrew Thompson
    http://www.athompson.info/andrew/

    Message posted via JavaKB.com
    http://www.javakb.com/Uwe/Forums.aspx/java-general/200710/1
     
    Andrew Thompson, Oct 13, 2007
    #7
  8. Guest

    how com i dont get unifrom distrobution wit this

    import java.util.Random;

    public class DiceRollerSimulation
    {
    public static void main(String[] args)
    {
    Random dieOne = new Random();
    Random dieTwo = new Random();
    int[] rollMap = new int[11];
    for(int i = 0; i < 500; i++)
    {
    rollMap[dieOne.nextInt(6) + dieTwo.nextInt(6)]++;
    }
    for(int out = 2; out < 13; out++)
    {
    System.out.println(out + " " + rollMap[out - 2] + " " +
    Math.round((((double)rollMap[out - 2]/500)) * 100.0));
    }
    }
    }
     
    , Oct 13, 2007
    #8
  9. wrote:
    > how com i dont get unifrom distrobution wit this


    Since this is a mathematical observation, it won't give anything away if
    I just explain it to you:

    There are 36 possible combinations:
    1,1 1,2 1,3 1,4 1,5 1,6
    2,1 2,2 2,3 2,4 2,5 2,6
    3,1 3,2 3,3 3,4 3,5 3,6
    4,1 4,2 4,3 4,4 4,5 4,6
    5,1 5,2 5,3 5,4 5,5 5,6
    6,1 6,2 6,3 6,4 6,5 6,6

    Sum them up:
    2 3 4 5 6 7
    3 4 5 6 7 8
    4 5 6 7 8 9
    5 6 7 8 9 10
    6 7 8 9 10 11
    7 8 9 10 11 12

    Question: Is this a uniform distribution?

    --
    Beware of bugs in the above code; I have only proved it correct, not
    tried it. -- Donald E. Knuth
     
    Joshua Cranmer, Oct 13, 2007
    #9
  10. On Sat, 13 Oct 2007 14:20:36 -0000, wrote:
    > how com i dont get unifrom distrobution wit this


    Why do you expect a uniform distribution? There's only one way to get
    2, but 6 ways to get 7.

    /gordon

    --
     
    Gordon Beaton, Oct 13, 2007
    #10
  11. rossum Guest

    On Sat, 13 Oct 2007 14:20:36 -0000, wrote:

    >how com i dont get unifrom distrobution wit this
    >
    >import java.util.Random;
    >
    >public class DiceRollerSimulation
    >{
    > public static void main(String[] args)
    > {
    > Random dieOne = new Random();
    > Random dieTwo = new Random();

    Not a good idea, sometimes you will get two random number generators
    initialised to the same seed value and you will always get the same
    number on the two dice.

    Better is:

    Random rand = new Random();

    rollMap([rand.nextInt(6) + rand.nextInt(6)]++;

    In general you only need one instance of Random per program.

    > int[] rollMap = new int[11];
    > for(int i = 0; i < 500; i++)
    > {
    > rollMap[dieOne.nextInt(6) + dieTwo.nextInt(6)]++;
    > }
    > for(int out = 2; out < 13; out++)
    > {
    > System.out.println(out + " " + rollMap[out - 2] + " " +
    > Math.round((((double)rollMap[out - 2]/500)) * 100.0));
    > }
    > }
    >}

    As others have pointed out, you should not expect to get a uniform
    distribution from rolling two dice and adding the pip values. You can
    only make 2 in one way: 1 + 1. There are six ways to make 7: 1 + 6, 2
    + 5, 3 + 4, 4 + 3, 5 + 2 and 1 + 6. You should get roughly six times
    as many sevens as you get twos.

    rossum
     
    rossum, Oct 13, 2007
    #11
  12. Jeff Higgins Guest

    rossum wrote:
    > Better is:
    >
    > Random rand = new Random();
    >
    > rollMap([rand.nextInt(6) + rand.nextInt(6)]++;
    >
    > As others have pointed out,


    Good idea. Thanks to all who responded.
    hw
     
    Jeff Higgins, Oct 14, 2007
    #12
  13. Roedy Green Guest

    On Sat, 13 Oct 2007 14:20:36 -0000, wrote, quoted
    or indirectly quoted someone who said :

    >how com i dont get unifrom distrobution wit this


    The sum of two random numbers is no longer uniform.

    I gather you never played Monopoly as a child. It is rare to get a 2
    or a 12 throwing two dice because there is only one way to get that
    sum.

    You can get a 7 easily because you can get it with
    1 + 6, 2 + 5, 3 + 4 or the reverse.

    Do they teach the binomial distribution (which comes to approximate a
    Bell shaped normal curve) in your part of the world?
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Oct 14, 2007
    #13
  14. Roedy Green Guest

    On Sun, 14 Oct 2007 09:42:50 GMT, Roedy Green
    <> wrote, quoted or indirectly quoted
    someone who said :

    >The sum of two random numbers is no longer uniform.
    >
    >I gather you never played Monopoly as a child. It is rare to get a 2
    >or a 12 throwing two dice because there is only one way to get that
    >sum.
    >
    >You can get a 7 easily because you can get it with
    >1 + 6, 2 + 5, 3 + 4 or the reverse.
    >
    >Do they teach the binomial distribution (which comes to approximate a
    >Bell shaped normal curve) in your part of the world?


    To convince yourself what I am saying in true, buy or make yourself a
    pair of dice with two sugar cubes and a pencil. Throw and record the
    sum results in columns, on graph paper using a vertical stroke one
    square tall for each hit. You will see a bell shaped pattern emerge.

    This manual solution will map nicely onto what you do in Java with an
    array.
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Oct 14, 2007
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kyle H.
    Replies:
    3
    Views:
    2,693
    Kyle H.
    Sep 26, 2006
  2. Replies:
    1
    Views:
    720
    Patricia Shanahan
    Oct 16, 2007
  3. x1
    Replies:
    9
    Views:
    331
    Rick DeNatale
    Oct 12, 2006
  4. PerlFAQ Server
    Replies:
    0
    Views:
    215
    PerlFAQ Server
    Feb 1, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    206
    PerlFAQ Server
    Mar 26, 2011
Loading...

Share This Page