Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-03-2013, 05:25 AM   PM User | #1
comfroels
New Coder

 
Join Date: Jan 2012
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
comfroels is an unknown quantity at this point
Question Random Number Generator Methods Question

Hello Everyone,
I'm still pretty new to java and still learning but created a little console program to test you on how well you can calculate change.
It functions (not perfectly) but I know there are better ways to do what I did. Any suggestions to help me develop better technique? Also,
I thought I could use methods different times and have it return a different value. As you will see I had to make two different random number generators. Then since they were both currentTimeMillis() they were still returning the same values, so i switch one to nanoTime().
Here's the code
Code:
import java.util.Scanner;
import java.util.Random;


public class ChangeTest {
	
	public static void main(String[] args) {
		double number1 = 0.0;
		double number2 = 0.0;
		int numCorrect = 0;
		double answer = 0.0;
		double userAnswer = 0.0;
		for(int i = 1; i <= 10;++i){
		Scanner input = new Scanner(System.in);
		number1 = (double)Math.round(genNumber1()*100)/100;
		number2 = (double)Math.round(genNumber2()*100)/100;
		
		
		
		
		
		if (number1 > number2){
			answer = number1 - number2;
			System.out.println("What would be the change if the price was: $" + number2 + " and you were given: $" + number1);
			userAnswer = input.nextDouble();
			if (userAnswer == answer){
				++numCorrect;
				System.out.println("That is correct!");
			}
			else
				System.out.println("I'm sorry that is incorrect.");
			} 
		else
		{
			answer = number2 - number1;
			System.out.printf("What would be the change if the price was: $" + number1 + " and you were given: $" + number2);
			userAnswer = input.nextDouble();
			if (userAnswer == answer){
				numCorrect++;
				System.out.println("That is correct!");
			}
			else
				System.out.println("I'm sorry that is incorrect.");
			}
		}
		if(numCorrect == 10){
			System.out.println("Congratulations! You got a perfect score!!!");
		}
		else if (numCorrect >= 7) {
			System.out.println("Good work! You got " + numCorrect + " correct!!!");
			}
		else
		{
			System.out.println("You can do better! You got " + numCorrect + " correct...");
		}
		
		
	}
	
	
		static double genNumber1(){
		double num1;
		Random generator = new Random(System.currentTimeMillis());
		num1 = generator.nextDouble() * 50.0;
		return num1;
		}
		
		static double genNumber2(){
		double num1;
		Random generator = new Random(System.nanoTime());
		num1 = generator.nextDouble() * 50.0;
		return num1;
		}
		
		
	
}
So yeah, is there an easier way?
comfroels is offline   Reply With Quote
Old 01-08-2013, 08:05 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
When it comes to random, simply don't seed it. If you provide two random instances which happen to have the same seed, than the random numbers they draw would be the same:
PHP Code:
        Random r1 = new Random(5);
        
Random r2 = new Random(5);
        for (
int i 05; ++i)
        {
            
System.out.println("Random 1 pulled: " r1.nextInt());
            
System.out.println("Random 2 pulled: " r2.nextInt());
            
System.out.println();
        } 
So despite two completely separate instances of Random, both of them will randomly select the same numbers on each call to next. Simply don't seed it and that behaviour will disappear.
You should consider using integers to store these in. You can display them as floating by dividing by 100, but there is rounding violations that occur due to both limitation in hardware as well as base conversion (ie: 1/10 in base2 cannot be represented as an even number and so repeats infinitely). This can cause havoc although likely will not be a problem when dealing with such low fractional values (and more importantly, ones that do not operate on them).
If you're interested in these, you can find a large number of resources if you search for computational mathematics. I'd have to dust the old book off otherwise :P
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 05:29 PM.


Advertisement
Log in to turn off these ads.