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

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...
DESCRIPTION Complete the given program to simulate the roll of two 6-sided dice and count the...
DESCRIPTION Complete the given program to simulate the roll of two 6-sided dice and count the number of rolls of each possible roll value (2 to 12). The number of rolls will be input by the user. Count the rolls of each type and then "draw" a histogram by printing a "*" for each roll of each type. The shape of the histogram will follow the "probability distribution" for large numbers of rolls, i.e., it will show more rolls for...
Java 176 Lottery Program in Word. Using ArrayLists to Simulate a Lottery Program Simulate a Lottery...
Java 176 Lottery Program in Word. Using ArrayLists to Simulate a Lottery Program Simulate a Lottery Drawing by choosing 7 numbers at random from a pool containing 30 numbers Each time a number is selected, it is recorded and removed from the pool The pool values are 00 to 29 inclusive Your program must output each selected number from the drawing using a two-digit format. For Example, the number 2 would be represented by 02 on the “Picked” line. The...
You will write a Java Application program to perform the task of generating a calendar for...
You will write a Java Application program to perform the task of generating a calendar for the year 2020. You are required to modularize your code, i.e. break your code into different modules for different tasks in the calendar and use method calls to execute the different modules in your program. Your required to use arrays, ArrayList, methods, classes, inheritance, control structures like "if else", switch, compound expressions, etc. where applicable in your program. Your program should be interactive and...
Write a program IN JAVA that asks the user for a number. The program should check...
Write a program IN JAVA that asks the user for a number. The program should check the number to ensure that it is valid (if not, the user should enter a valid number to continue.) The program should print out all of the prime numbers from 2 up to the number, with up to 10 numbers per line. (Recall: A prime number is a number that is only divisible by itself and 1.) The code should ask the user if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT