In: Computer Science
Write a Java program 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. Your application should roll the dice 36,000,000 times. Store the results of each roll in a two-dimensional array as follows:
here's my code so far, i just need to know how to print the percentages
package rolldie;
import java.security.SecureRandom;
public class Rolldie {
public static void main(String[]
args) {
// TODO
Auto-generated method stub
SecureRandom
randomNumbers = new SecureRandom();
int[] frequency
= new int[13];
for (int counter =1; counter<=3600000;
counter++)
{
int roll1 = 1 + randomNumbers.nextInt(6);
int roll2 = 1 + randomNumbers.nextInt(6);
int total = roll1 + roll2;
frequency[total]++;
}
System.out.println();
System.out.println ("Total\t#of Times Rolled\t% of Times
Rolled ");
for (int counter=2; counter <
frequency.length; counter++)
System.out.printf("%d\t%d%n",counter, frequency[counter],
(frequency[counter]* 100 / 3600000));
}
}
import java.security.SecureRandom;
public class Rolldie {
public static void main(String[] args) {
// TODO Auto-generated method stub
SecureRandom randomNumbers = new SecureRandom();
int[] frequency = new int[13];
for (int counter =1; counter<=3600000; counter++)
{
int roll1 = 1 + randomNumbers.nextInt(6);
int roll2 = 1 + randomNumbers.nextInt(6);
int total = roll1 + roll2;
frequency[total]++;
}
System.out.println();
System.out.println ("Total\t#of Times Rolled\t% of Times Rolled ");
for (int counter=2; counter < frequency.length; counter++)
// we should divide it with 3600000.0 as decimal value and we should use format specifier to display
System.out.printf("%5d %17d %20.2f\n",counter, frequency[counter],
(frequency[counter]/ 3600000.0 * 100));
}
}
NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.
Please Like and Support me as it helps me a lot