Your friend plays a game of chance based on the sum of the rolls of three six-sided dice. Clearly the totals can range between three (all ones) and eighteen (all sixes), however, your friend decides he wants to know the probability of each value appearing. Rather than do the mathematics he decides to find out by trials. He rolls the dice 1,000 times and counts the number of threes that occur. Then he rolls the dice another 1,000 times and counts the fours and so forth. You decide you’d rather him play Frisbee Golf with you and so you decide to model his process using a program. Writing and running the program will be faster than his method of finding the probabilities. Your program should output the count of each number in sequence, just as he would do.

Respuesta :

Answer:

import java.util.*;

class ProbabilityOfRollingThreeDice

{

   public static void main(String[] args)

   {

      Random rnd = new Random();

      System.out.print("Enter a random seed: ");

      long seed = new Scanner(System.in).nextLong();

      rnd.setSeed(seed);

      for(int i = 3; i <= 18; i++)

      {

         int count = 0;

         

         for(int j = 0; j < 1000; j++)

         {

            int roll1 = rnd.nextInt(6) + 1;

            int roll2 = rnd.nextInt(6) + 1;

            int roll3 = rnd.nextInt(6) + 1;

            int sum = roll1 + roll2 + roll3;

            if(sum == i)

                 count++;

         }      

         System.out.println("The value " + i + " occurred " + count + " times.");

      }

   }

}

ACCESS MORE