Question

Write a java program that simulates the rolling of four dice. The program should use random...

Write a java program that simulates the rolling of four
dice. The program should use random number generator to roll dice. The
sum of the four values should then be calculated. Note that each die can
show an integer value from 1 to 6, so the sum of the four values will vary from 4 to 24, with 14 being the most frequent sum and 4 and 24 being the
least frequent sums. Your program should roll the dice 12,960 times. Use an
one dimensional array to tally the numbers of times each possible sum
appears. Print the result on console in tabular format. Check the totals are
reasonable. As an example, there are 6 ways to roll a 7, so approximately
1/6 of all the rolls should be 7

0 0
Add a comment Improve this question Transcribed image text
Answer #1


public class Roll4Dice {

   public static void main(String[] args) {
       int [] sumOfRolls = new int[21];
       int dice1,dice2,dice3,dice4;
       for (int i=0;i<12960;i++) {
           dice1=1 +(int)(Math.random()*6);
           dice2=1 +(int)(Math.random()*6);
           dice3=1 +(int)(Math.random()*6);
           dice4=1 +(int)(Math.random()*6);
           sumOfRolls[dice1+dice2+dice3+dice4-4]+=1;
       }
       System.out.printf("Sum of rolls        Frequency ");
       for(int i=4;i<25;i++) {
           System.out.printf("    %3d            %7d ",i,sumOfRolls[i-4]);
       }

   }

}

Add a comment
Know the answer?
Add Answer to:
Write a java program that simulates the rolling of four dice. The program should use random...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • please do it in C++ Write a program that simulates the rolling of two dice. The...

    please do it in C++ Write a program that simulates the rolling of two dice. The program should use rand to roll the first die and should use rand again to roll the second die. 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 2and 12...

  • ( Java Programming ) Write an application to simulate the rolling of two dice. The application...

    ( Java Programming ) 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...

  • 1T (startIndex< size) ( 22 23 someFunction(b, startIndex +1, size); printf("%d ", b[startIndex]); 24 25 26...

    1T (startIndex< size) ( 22 23 someFunction(b, startIndex +1, size); printf("%d ", b[startIndex]); 24 25 26 (Dice Rolling) Write a program that simulates the rolling of two dice. The program should rand twice to roll the first die and second die, respectively. The sum of the two values should then be calculated. [Note: Because each die can show an integer value from 1 to 6, then the sum of the two values will vary from 2 to 12, with 7...

  • Please i need helpe with this JAVA code. Write a Java program simulates the dice game...

    Please i need helpe with this JAVA code. Write a Java program simulates the dice game called GAMECrap. For this project, assume that the game is being played between two players, and that the rules are as follows: Problem Statement One of the players goes first. That player announces the size of the bet, and rolls the dice. If the player rolls a 7 or 11, it is called a natural. The player who rolled the dice wins. 2, 3...

  • (MATLAB) Write a program that simulates a dice roll by picking a random number from 1-6...

    (MATLAB) Write a program that simulates a dice roll by picking a random number from 1-6 and then picking a second random number from 1-6.    How many times do you get two 1’s. What many times do you get two 1’s if you simulate 10,000 rolls of a pair of dice?

  • Write a c++ program that simulates a million of games in craps. I am having a...

    Write a c++ program that simulates a million of games in craps. I am having a trouble getting to loop a million of times. I am trying to use a const for a million. This is the code so far. #include <iostream> #include <cstdlib>// contains prototypes for functions srand and rand #include <ctime>// contains prototype for function time #include <iomanip> using namespace std; int rollDice(); // rolls dice, calculates and displays sum void printstats(); int totroll = 0, games, point,...

  • Dice Rollers( write java) Write a complete thread class to represent rolling dice. The thread tak...

    Dice Rollers( write java) Write a complete thread class to represent rolling dice. The thread takes a parameter that represents how many dice are being rolled at once and how many times to roll. When the thread is run, it rolls the specified number of dice the specified number of times. The thread counts for how many rolls the number on all dice match. This value is saved as instance data. Write a program to create and run several threads...

  • In python 3, Write a program in Python that simulates the roll of two dice. The...

    In python 3, Write a program in Python that simulates the roll of two dice. The first die is a 6-sided die. The second die is an 8-sided die. Generate a random number for a die and store it in variable number1. Generate a random number for a die and store it in variable number2. Note: Submit your code with the print("You rolled two dice:", number1, "and", number2) statement.

  • PYTHON: Dice Rolling Write a script that takes input for the number of dice to roll...

    PYTHON: Dice Rolling Write a script that takes input for the number of dice to roll and for the number of sides for the dice. Roll that many n-sided dice and store the values an array. Take the number of sides of the dice and the number of dice as typed inputs from the user. Output should be a comma separated list of the die numbers with no spaces like this: 6,4,1,3,1 Write a script that will simulate the roll...

  • Write a Python (version 3.5.2) program that simulates how often certain hands appear in Yahtzee. In...

    Write a Python (version 3.5.2) program that simulates how often certain hands appear in Yahtzee. In Yahtzee, you roll five dice and then use those dice to make certain combinations. For example, the Yahtzee combination is formed by all five dice having the same value. A large straight occurs when all of the dice are in consecutive order (e.g., 1,2,3,4,5 or 2,3,4,5,6). If you haven’t played Yahtzee, you can find out more from various online sources such as Wikipedia. Once...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT