![]() |
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; |
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:
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 |
| All times are GMT +1. The time now is 09:03 AM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.