Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Random Number Generator Methods Question
 User Name Remember Me? Password

Before you post, read our: Rules & Posting Guidelines

 Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
 01-03-2013, 06:25 AM PM User | #1 comfroels New Coder   Join Date: Jan 2012 Posts: 13 Thanks: 0 Thanked 0 Times in 0 Posts 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?
 01-08-2013, 09:05 PM PM User | #2 Fou-Lu God Emperor     Join Date: Sep 2002 Location: Saskatoon, Saskatchewan Posts: 16,597 Thanks: 4 Thanked 2,597 Times in 2,566 Posts 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

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 11:33 AM.

 AdvertisementLog in to turn off these ads.