COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Dice Frequency Simulation

• Follow

Hi I'm new to JAVA Programming and need help with my homework!The assignment:  Design and implement a simple, well-documented JAVAprogram that uses random numbers to simulate throwing a pair of diceand recording the total score thrown.  That is, each roll will throwtwo 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 ascore 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 andshould sum up how many times each score was thrown.  That is, how manyof the 500 times was  a total of 2 thrown, how many times was a totalof 3 thrown, etc.  You will need one accumulator for each of the 11possible scores. Afer all 500 rolls have been thrown and counted, theprogram should report for each possible score (2-12), how many timesit was thrown and what % of the time it  was thrown.  This programwill have no input.  It should produce output similar to thefollowing:Total          Number            PercentRolled       of Times           of TimeSo 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 donext!!
 0
Reply cb17890 (3) 10/13/2007 4:58:30 AM

cb17890@yahoo.com 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

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
 0
Reply Patricia 10/13/2007 5:05:57 AM

On Fri, 12 Oct 2007 21:58:30 -0700, cb17890@yahoo.com 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 withthose particular loop values. How many iterations are you expecting?You might start by writing a program that just rolls one die once./gordon--
 0
Reply Gordon 10/13/2007 7:00:55 AM

On Fri, 12 Oct 2007 21:58:30 -0700, cb17890@yahoo.com wrote, quoted orindirectly quoted someone who said :>Hi I'm new to JAVA Programming and need help with my homework!see http://mindprod.com/jgloss/homework.htmlJust posting your assignment and hoping others will do it for youwon't fly very far, partly because you would learn nothing, subvertingyour enormous investment in time and money to have a school teach youJava.-- Roedy Green Canadian Mind ProductsThe Java Glossaryhttp://mindprod.com
 0
Reply Roedy 10/13/2007 7:04:54 AM

On Fri, 12 Oct 2007 21:58:30 -0700, cb17890@yahoo.com wrote, quoted orindirectly quoted someone who said :>Hi I'm new to JAVA Programming and need help with my homework!see http://mindprod.com/jgloss/randomnumbers.html-- Roedy Green Canadian Mind ProductsThe Java Glossaryhttp://mindprod.com
 0
Reply Roedy 10/13/2007 8:43:12 AM

On Fri, 12 Oct 2007 21:58:30 -0700, cb17890@yahoo.com 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
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
 0
Reply rossum 10/13/2007 10:17:53 AM

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 Thompsonhttp://www.athompson.info/andrew/Message posted via JavaKB.comhttp://www.javakb.com/Uwe/Forums.aspx/java-general/200710/1
 0
Reply Andrew 10/13/2007 10:31:10 AM

how com i dont get unifrom distrobution wit thisimport 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));    }  }}
 0
Reply hwdoer01 10/13/2007 2:20:36 PM

hwdoer01@gmail.com wrote:> how com i dont get unifrom distrobution wit thisSince 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,62,1 2,2 2,3 2,4 2,5 2,63,1 3,2 3,3 3,4 3,5 3,64,1 4,2 4,3 4,4 4,5 4,65,1 5,2 5,3 5,4 5,5 5,66,1 6,2 6,3 6,4 6,5 6,6Sum 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 12Question: Is this a uniform distribution?-- Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth
 0
Reply Joshua 10/13/2007 3:16:32 PM

On Sat, 13 Oct 2007 14:20:36 -0000, hwdoer01@gmail.com wrote:> how com i dont get unifrom distrobution wit thisWhy do you expect a uniform distribution? There's only one way to get2, but 6 ways to get 7./gordon--
 0
Reply Gordon 10/13/2007 3:17:24 PM

On Sat, 13 Oct 2007 14:20:36 -0000, hwdoer01@gmail.com 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 generatorsinitialised to the same seed value and you will always get the samenumber 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 uniformdistribution from rolling two dice and adding the pip values.  You canonly 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 timesas many sevens as you get twos.rossum
 0
Reply rossum 10/13/2007 9:55:09 PM

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
 0
Reply Jeff 10/13/2007 11:46:58 PM

On Sat, 13 Oct 2007 14:20:36 -0000, hwdoer01@gmail.com wrote, quotedor indirectly quoted someone who said :>how com i dont get unifrom distrobution wit thisThe sum of two random numbers is no longer uniform.I gather you never played Monopoly as a child.  It is rare to get a 2or a 12 throwing two dice because there is only one way to get thatsum.You can get a 7 easily because you can get it with1 + 6,  2 + 5,  3 + 4 or the reverse.Do they teach the binomial distribution (which comes to approximate aBell shaped normal curve) in your part of the world?-- Roedy Green Canadian Mind ProductsThe Java Glossaryhttp://mindprod.com
 0
Reply Roedy 10/14/2007 9:42:50 AM

On Sun, 14 Oct 2007 09:42:50 GMT, Roedy Green<see_website@mindprod.com.invalid> wrote, quoted or indirectly quotedsomeone 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 apair of dice with two sugar cubes and a pencil.  Throw and record thesum results in columns, on graph paper using a vertical stroke onesquare 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 anarray.-- Roedy Green Canadian Mind ProductsThe Java Glossaryhttp://mindprod.com
 0
Reply Roedy 10/14/2007 9:48:05 AM

13 Replies
251 Views

(page loaded in 0.189 seconds)

Similiar Articles:

7/17/2012 8:22:19 PM