The moose likes Beginning Java and the fly likes Simulation of Rolling 2 Dice... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Simulation of Rolling 2 Dice..." Watch "Simulation of Rolling 2 Dice..." New topic

Simulation of Rolling 2 Dice...

Kelsey kelskjs
Ranch Hand

Joined: Nov 07, 2003
Posts: 36
I need help with this one. I am in a JAVA class and am really confused with this....can anyone show me the code that would help me with this?
"Write an applet to simulate the rolling of two dice. The program should use Math.random once to roll the first dice and again to roll the second die. The sum of the two values should then be calculated. Each die can show an integer value from 1 to 6, so the sum of the values will vary from 2 to 12, with 7 being the most frequent sum and 2 and 12 being the least frequent sums. Your program should roll the dice 36,000 times. Use a one-dimensional array to tally the numbers of times each possible sum appears. Display the results in a JTextArea in a tabular format. Also, determine whether the totals are reasonable (i.e. there are six ways to roll a 7, so approximately one-sixth of the rolls should be 7). The applet should use the GUI techniques. Provide a JButton to allow the user of the applet to roll the dice another 36,000 times. The applet should reset the elements of the one-dimensional array to zero before rolling the dice again.

HELP!!! Please! Thank you.
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

We're not really in the habit of doing homework for you, but we will provide a few pointers. First, break the problem down into separate parts and test them. Don't try to write the whole things at once. There are many parts to this, each of which can be tested seperately. Remember, every class in your application can have a "main" method; it's just the class that you indicate on the command line whose main method gets called.
First, simulate the rolls of dice and adding the sums. This can easily be tested through command-line tests.
Second, simulate acquiring the sums and adding them to an array. Note that the array only need be 13 long (well, it only need be 11 long, but in this instance it's simpler to ignore indicies 0 and 1 rather than eliminate them...)
You should just start with rolling the dice 10 times, printing out the sums, and then printing out the array. That way you can easily verify that your program is working correctly before trying a 36,000 rolls.
Third, run the statical analysis. Here's the breakdown of stats for two-dice rolls:
2: 1/36
3: 2/36
4: 3/36
5: 4/36
6: 5/36
7: 6/36
8: 5/36
9: 4/36
10: 3/36
11: 2/36
12: 1/36
Finally, put the results in the GUI asked for.
Try writing the code yourself, and if you run into specific problems, post the code here and we will gladly help you debug it -- we just want to see your effort and what approach you are taking.

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
I agree. Here's the link:
subject: Simulation of Rolling 2 Dice...
It's not a secret anymore!