Closed Thread
Results 1 to 2 of 2

Thread: Help with simple project..?

  1. #1
    minakrn's Avatar
    minakrn is offline Newbie
    Join Date
    Sep 2007
    Posts
    1
    Rep Power
    0

    Smile Help with simple project..?

    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.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Sep 2007
    Location
    Utah, USA
    Posts
    22
    Blog Entries
    1
    Rep Power
    0
    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.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Wanted: Paid Project - Free Marketing Project
    By oct2010 in forum Request Services
    Replies: 4
    Last Post: 10-29-2010, 12:47 AM
  2. Project: ionFiles - Joomla Simple File Download
    By Jordan in forum Community Projects
    Replies: 370
    Last Post: 08-30-2010, 10:45 PM
  3. Replies: 0
    Last Post: 03-18-2010, 04:42 PM
  4. Replies: 6
    Last Post: 11-06-2008, 10:24 AM
  5. Project for a simple password cracker
    By waf in forum Visual Basic Programming
    Replies: 2
    Last Post: 03-22-2007, 02:19 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts