asked 565 views
0 votes
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

asked
User Dun Peal
by
8.4k points

1 Answer

7 votes
01 import java.util.Random;02 03 public class RollDice_Mod {04 05 static Random randomNumbers = new Random();06 static final int SIDES_ON_DICE = 6;07 static final int REPETITIONS = 36000000;08 09 public static void main(String[] args) {10 int[] frequency = countRollFrequencies(REPETITIONS);11 displayFrequencies(frequency);12 } 13 14 static int[] countRollFrequencies(int repetitions) {15 int maxRoll = SIDES_ON_DICE + SIDES_ON_DICE;16 int[] result = new int[maxRoll + 1];17 for(int i = 0; i < repetitions; i++){18 ++result[rollTwoDice()];19 }20 return result;21 }2223 static int rollTwoDice() {24 return rollDice() + rollDice();25 }2627 static int rollDice(){28 return 1 + randomNumbers.nextInt(SIDES_ON_DICE);29 }3031 static void displayFrequencies(int[] frequency) {32 int minRoll = 2;33 for(int i = minRoll; i < frequency.length; i++){34 System.out.printf("%4d%10d\\", i, frequency[i]); 35 }36 }37 38}
answered
User Nico Van Belle
by
7.6k points

No related questions found

Welcome to Qamnty — a place to ask, share, and grow together. Join our community and get real answers from real people.