/* * Solution for Homework 1 * by Sriram Pemmaraju, 9/11 * I started from Ezra Sidran's solution to Quiz 1 */ import java.io.*; import java.util.*; // Note: you do not need to import java.util.Random because java.util.* includes this class TwoRolls{ private static BufferedReader stdin = new BufferedReader( new InputStreamReader( System.in ) ); // main method public static void main (String args[]) { // Prompt the user System.out.println( "Type in the size of the dice. " ); try{ // Read a line of text from the user. String input = stdin.readLine(); // converts a String into an int value int number = Integer.parseInt( input ); Random myRandom = new Random(); // Declare an array and allocate the right amount of memory to // it; for two n-sided dice, the number of possible sums is // 2*n-1. Initialize the array to all zeroes. int[] ndigits = new int[2*number-1]; for (int i = 0; i < number; i++) ndigits[i] = 0; // Test the random number generator a whole lot for (long i=0; i < 100000; i++) { // generate a new random number between 0 and number-1 // adding one gives us a random number between 1 and number int x = 1 + myRandom.nextInt(number); int y = 1 + myRandom.nextInt(number); // The fact that the sum is x + y is stored in slot // with index x + y - 2. ndigits[x+y-2]++; }// end for i for (int i = 0; i < 2*number-1; i++) System.out.println((i+2)+": " + ndigits[i]); } // End try catch(java.io.IOException e) { System.out.println(e); } } // end of Main } // end of RandomTest class