Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2007

Tip: Looking for answers? Try searching our database.

Dice Frequency Simulation

Thread view: 
cb17890@yahoo.com - 13 Oct 2007 05:58 GMT
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!!
Patricia Shanahan - 13 Oct 2007 06:05 GMT
> Hi I'm new to JAVA Programming and need help with my homework!
>
[quoted text clipped - 20 lines]
> 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
Gordon Beaton - 13 Oct 2007 08:00 GMT
> 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

--
Roedy Green - 13 Oct 2007 08:04 GMT
>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.

Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roedy Green - 13 Oct 2007 09:43 GMT
>Hi I'm new to JAVA Programming and need help with my homework!

see http://mindprod.com/jgloss/randomnumbers.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

rossum - 13 Oct 2007 11:17 GMT
>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
[quoted text clipped - 4 lines]
>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
Andrew Thompson - 13 Oct 2007 11:31 GMT
>>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>

Signature

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

hwdoer01@gmail.com - 13 Oct 2007 15:20 GMT
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));
   }
 }
}
Joshua Cranmer - 13 Oct 2007 16:16 GMT
> 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?

Signature

Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth

Gordon Beaton - 13 Oct 2007 16:17 GMT
> 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

--
rossum - 13 Oct 2007 22:55 GMT
>how com i dont get unifrom distrobution wit this
>
[quoted text clipped - 6 lines]
>    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++)
[quoted text clipped - 8 lines]
>  }
>}
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
Jeff Higgins - 14 Oct 2007 00:46 GMT
> Better is:
>
[quoted text clipped - 3 lines]
>
> As others have pointed out,

Good idea. Thanks to all who responded.
hw
Roedy Green - 14 Oct 2007 10:42 GMT
>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?
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roedy Green - 14 Oct 2007 10:48 GMT
On Sun, 14 Oct 2007 09:42:50 GMT, Roedy Green
<see_website@mindprod.com.invalid> wrote, quoted or indirectly quoted
someone who said :

>The sum of two random numbers is no longer uniform.
>
[quoted text clipped - 7 lines]
>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.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.