Lost Password?


Go Back   CodeCall Programming Forum > Software Development > Java Help

Java Help Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-05-2007, 10:47 PM
minakrn's Avatar   
minakrn minakrn is offline
Newbie
 
Join Date: Sep 2007
Posts: 1
Rep Power: 0
minakrn has disabled reputation
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-06-2007 at 01:07 AM. Reason: Added code-tags.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 09-12-2007, 05:32 PM
Nathandelane's Avatar   
Nathandelane Nathandelane is offline
Newbie
 
Join Date: Sep 2007
Location: Utah, USA
Posts: 10
Rep Power: 0
Nathandelane is on a distinguished road
Default

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 05:47 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Project: ionFiles - Joomla Simple File Download - Mirror 2 Jordan ionFiles 6 11-06-2008 01:24 PM
A cool project idea. AlexanderRybak Community Projects 3 02-11-2008 03:37 AM
Project for a simple password cracker waf Visual Basic Programming 2 03-22-2007 05:19 PM
Contributing to a project Jordan Community Projects 7 09-15-2006 05:43 PM
Community Project! - Recent project gone sour Crane C# Programming 5 09-09-2006 03:13 PM


All times are GMT -5. The time now is 12:05 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads