Question

In: Computer Science

Write a Java program to simulate the rolling of two dice. The application should use an...

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));
       }
   }

Solutions

Expert Solution

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


Related Solutions

write c program to generate 6 numbers to simulate rolling of a dice . use while...
write c program to generate 6 numbers to simulate rolling of a dice . use while loop to run 100 and 10000 times. print out how many times it generates 1, 2,3,4,5,6.
. Dice rolling: In c++Write a program that simulates the rolling of two dice. The sum...
. Dice rolling: In c++Write a program that simulates the rolling of two dice. The sum of the two values should then be calculated. [Note: Each die can show an integer value from 1 to 6, so the sum of the two values will vary from 2 to 12, with 7 being the most frequent sum and 2 and 12 being the least frequent sums.] The following table shows the 36 possible combinations of the two dice. Your program should...
Java Programming Activity Description: This lab requires you to simulate the rolling of two dice. Two...
Java Programming Activity Description: This lab requires you to simulate the rolling of two dice. Two dice consisting of 6 sides are rolled. Write a program that simulates this. The user will be asked if he/she wishes to continue the roll of dice. If the answer is, “Y” or “y”, then your application will continue to roll the two dice. Each roll of the dice must occur 6 times, each time the user agrees to continue (see sample output). Use...
1. Let’s use Excel to simulate rolling two dice and finding the rolled sum. • Open...
1. Let’s use Excel to simulate rolling two dice and finding the rolled sum. • Open a new Excel document. • Click on cell A1, then click on the function icon fx and select Math&Trig, then select RANDBETWEEN. • In the dialog box, enter 1 for bottom and enter 6 for top. • After getting the random number in the first cell, click and hold down the mouse button to drag the lower right corner of this first cell, and...
Lets use Excel to simulate rolling two 8-sided dice and finding the rolled sum. • Open...
Lets use Excel to simulate rolling two 8-sided dice and finding the rolled sum. • Open a new Excel document. • Click on cell A1, then click on the function icon fx and select Math&Trig, then select RANDBETWEEN. • In the dialog box, enter 1 for bottom and enter 8 for top. • After getting the random number in the first cell, click and hold down the mouse button to drag the lower right corner of this first cell, and...
In Java, Write a Java program to simulate an ecosystem containing two types of creatures, bears...
In Java, Write a Java program to simulate an ecosystem containing two types of creatures, bears and fish. The ecosystem consists of a river, which is modeled as a relatively large array. Each cell of the array should contain an Animal object, which can be a Bear object, a Fish object, or null. In each time step, based on a random process, each animal either attempts to move into an adjacent array cell or stay where it is. If two...
1. Write a program which simulates rolling dice. It should: Display the random number rolled Ask...
1. Write a program which simulates rolling dice. It should: Display the random number rolled Ask the user if they want to roll again. If they don't want to roll again, the program should end. If they do want to roll again, the new number should display. At a minimum, the die should have 6 sides, but if you want to use a d12 or d20, that is fine too. USE PYTHON
R Exercise 1. (a) Simulate the rolling of two dice 10,000 times. (b) Identify which rolls...
R Exercise 1. (a) Simulate the rolling of two dice 10,000 times. (b) Identify which rolls of the dice are in the event A, the dice add up to a perfect square (4 or 9). Determine what proportion of the 10,000 rolls are in A. (c) Identify which rolls of the dice are in the event B, the dice add up to an even number. Determine what proportion of the 10,000 rolls are in B. (d) Find out which rolls...
Write a program in C++ called RollDice.cpp that simulates rolling a pair of dice until the...
Write a program in C++ called RollDice.cpp that simulates rolling a pair of dice until the total on the dice comes up to be a given number. Ask the user for the number that you are rolling for. To have your program roll two dice, use the rand() function this way: die1 = rand() % 6 + 1; die2 = rand() % 6 + 1; Your program then computes and prints the number of rolls it takes to get the...
Write a program to simulate rolling a six-sided fair die. Allow the user to enter the...
Write a program to simulate rolling a six-sided fair die. Allow the user to enter the number of rolls. Your program should use rand() to get the result of die rolling. Compute the number of occurrences for each number roll. Calculate the percentage for each number roll. Out put the results in the following format. Use a do while loop to allow the user to repeat the process. Here is the sample run for your program: Please enter the number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT