Hi,
I am new programmer that need help with this idea. I am trying to write a simple simulation for the process of rolling two n-sided dice and recording the sum of the outcomes. However, the sum of the outcomes are duplicated. How can I get rid of this?
Here is my program:
Code:// import necessary package import java.util.*; // declare class class TwoRolls { public static void main (String args[]) { // print introduction message System.out.println("\n\n\t\t\t\t\t=== RANDOM TWO ROLL IMPLEMENTATION PROGRAM === \n"); // declare and initialize local variables int entry; int double_entry; int counter_1 = 0; int counter_2 = 0; // declare local array int sum[]; // prompt user for input System.out.print( "Enter your desirable positive n-value: " ); // declare scanner object Scanner input = new Scanner( System.in); // read input with scanner object entry = input.nextInt(); // conditional exception when input is a negative n-value if (entry <= 0) { System.out.println( "\nThat is an invalid entry. "); System.out.print("\nTERMINATING PROGRAM"); } // conditional exception when input is a positive n-value else { System.out.println("\nYour new n-value entry: " + entry); // initialize variable by multiplying input by two double_entry = entry * 2; // call reference to proper method sum = randomRoll(entry); System.out.print("\n\n[SUM]\t\t\t[APPEARANCE]"); System.out.println("\n------------------------------------------------//"); // outer loop statement that increments counter to read the overall array for (int counter = 0; counter < sum.length; counter++) { // re-initialize variable counter_2 = 0; // inner loop statement that increments counter to check for similar values for(counter_1 = 0; counter_1 < sum.length; counter_1++) { // conditional exception when similar value is found if (sum[counter] == sum[counter_1]) { // increment counter counter_2++; } } // print output according to format System.out.println(sum[counter] + "\t\t\t" + counter_2); } } } // terminate main function // method call to randomRoll public static int[] randomRoll(int n_value) { // declare local variables int first_roll; int second_roll; // declare and initialize local array in terms of number of attempts int[] sum_array = new int[10]; // loop statement that increments counter in accordance to the number of attempts for (int counter = 0; counter < sum_array.length; counter++) { // mark each roll variable to a random value according to specification first_roll = 1 + (int)(Math.random() * n_value); second_roll = 1 + (int)(Math.random() * n_value); // mark the sum sum_array[counter] = first_roll + second_roll; // print informational output in regards to each roll and total sum System.out.println("\n\t\t\tFIRST ROLL = " + first_roll); System.out.println("\t\t\tSECOND ROLL = " + second_roll); System.out.println("\n\t\t\t\t\t\tTOTAL SUM = " + sum_array[counter]); } // recall result back to the main function return sum_array; } // terminate randomRoll method } // terminate TwoRolls class
Hope anyone can help me. Thank you~
Last edited by v0id; 09-05-2007 at 10:07 PM. Reason: Added code-tags.
Okay great problem - here's what I would change, and it should be fairly simple - instead of using Math.random(), use java.util.Random with a seed. You can create a seed by getting the current datetime from java.util.Calendar like this: long seed = java.util.Calendar.getInstance().getTimeInMillis() ; Then plug that seed into the constructor for a new Random object. So basically instead of:
first_roll = 1 + (int)(Math.random() * n_value);
second_roll = 1 + (int)(Math.random() * n_value);
do:
long seed = java.util.Calendar.getInstance().getTimeInMillis() ;
Random r = new java.util.Random(seed);
first_roll = 1 + (int)(r.nextInt() * n_value);
second_roll = 1 + (int)(r.nextInt() * n_value);
Does that help you out? Does that work?
Nathan
Last edited by Nathandelane; 09-12-2007 at 02:47 PM.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks