Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

Thread: Random Number Generator Methods Question

1. 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;
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){
System.out.println("What would be the change if the price was: \$" + number2 + " and you were given: \$" + number1);
++numCorrect;
System.out.println("That is correct!");
}
else
System.out.println("I'm sorry that is incorrect.");
}
else
{
System.out.printf("What would be the change if the price was: \$" + number1 + " and you were given: \$" + number2);
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?

• 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 = 0; i < 5; ++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

•

Posting Permissions

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