(Dice Rolling) Write an application to simulate the rolling of two dice. The app
ID: 3587394 • Letter: #
Question
(Dice Rolling) Write an application to simulate the rolling of two dice. The application
should use an object of class Random once to roll the first die 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 the
least frequent. Figure 7.28 shows the 36 possible combinations of the two dice. Your application
should roll the dice 36,000,000 times. Use a one-dimensional array to tally the number of times
each possible sum appears. Display the results in tabular format.
Example output looks like this:
Sum Frequency Percentage
2 1020 2.83
3 1946 5.41
4 3006 8.35
5 4021 11.17
6 4940 13.72
7 6003 16.68
8 5011 13.92
9 4057 11.27
10 3025 8.40
11 1908 5.30
12 1063 2.95
Code looks like this:
import java.util.Random;
import java.util.Arrays;
public class DiceRolling {
public static void main(String args[]) {
Random dice = new Random();
int[] diceRolls = new int[36];
int roll1;
int roll2;
// Loops through total number of desired dice rolls
for(int count = 0; count < 36000000; count++) {
roll1 = (dice.nextInt(6) + 1);
roll2 = (dice.nextInt(6) + 1);
int row = 0;
int column = 0;
// Roll1 determines row
if(roll1 == 1) {
row = 30;
} else if(roll1 == 5){
row = 24;
} else if(roll1 == 4){
row = 18;
} else if(roll1 == 3){
row = 12;
} else if(roll1 == 2){
row = 6;
} else {
row = 0;
}
// Roll2 determines column
if(roll2 == 6) {
column = 5;
} else if(roll2 == 5) {
column = 4;
} else if(roll2 == 4) {
column = 3;
} else if(roll2 == 3) {
column = 2;
} else if(roll2 == 2) {
column = 1;
} else {
column = 0;
}
// Column + Row = value that corresponds to a coordinate
diceRolls[(row + column)] += 1;
}
// Print values
System.out.printf(" %d %d %d %d %d %d ", 1, 2, 3, 4, 5, 6); // column label
System.out.printf("----------------------------------------------------------------------");
for(int table = 0; table < diceRolls.length; table++) {
if( (table) % 6 == 0) {
System.out.printf(" %d| ", (table / 6 + 1) ); // row label
}
System.out.printf("%d ", diceRolls[table]);
}
// Formatting
System.out.println("");
}
}
Explanation / Answer
TwoDice36000000.java
import java.util.Random;
public class TwoDice36000000 {
public static void main(String[] args) {
final int ROLLS = 36000000;
final int SIZE = 13;
//Declaring variables
int x; // first die
int y; // second die
double percentage;
//Creating an array
int freq[] = new int[SIZE];
//Creating an random class object
Random r = new Random();
// roll dice 36000000 times
/* Write a for loop that iterates ROLLS times. Randomly
generate values for x (i.e., die1) and y (i,e, die2)
and increment the appropriate counter in array sum that
corresponds to the sum of x and y */
for (int i = 0; i < ROLLS; i++) {
x = r.nextInt(6) + 1;
y = r.nextInt(6) + 1;
freq[x + y]++;
}
//Displaying the table
System.out.println("Sum Frequency Percentage");
for (int i = 2; i < 13; i++) {
percentage = (((double) freq[i]) / ROLLS) * 100;
System.out.printf("%d %5d %.2f ", i, +freq[i], percentage);
}
}
}
__________________
Output:
Sum Frequency Percentage
2 1001425 2.78
3 2000610 5.56
4 3001942 8.34
5 3999243 11.11
6 4996909 13.88
7 5998160 16.66
8 4998522 13.88
9 4003259 11.12
10 2999344 8.33
11 1999833 5.56
12 1000753 2.78
_____________Could you rate me well.Plz .Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.