Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: random number generator

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default random number generator

    I am new to java and have been asked to write a program that will generate random numbers between 10 and 20 and then stop when 18 is generated. Have been told to use a while statement and also to use Maths.Random and Maths.Round


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: random number generator

    Hello java3,

    You must of learnt the basics by now? Please create a class and show us the code you have attempted.

    Show us where you are stuck and we can then go from there.

    Heres some help with the while loop: http://www.javaprogrammingforums.com...ava-loops.html
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: random number generator

    Ok this is what I've done. Like I said I am a complete novice to programming! When I run it it just keeps generating numbers non stop.

      public static void main(String[] Args)
      {
     
     
      // int nNum;
     
     
     
        do
        {
        	int n = 10;
        	double dNum = n*Math.random(); //a random double between 0 and 10
        	int nNum = (int)Math.round(dNum); //a random int between 0 and 10
        	//System.out.println(number);
        }
        while (nNum != 8);
        System.out.println("the end");
        }
     
      }

  4. #4
    Junior Member
    Join Date
    Feb 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: random number generator

    I was wanting the number generator to stop when an 8 was produced

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: random number generator

    Limit your base case to confirm your algorithm. Basically, your algorithm could theoretically generate numbers forever if it never returns an 8.

    So, to test your algorithm, instead of a base case of nNum != 8, try something like nNum < 3. This way you have a much more narrow window for the loop continuing and if the loop breaks you can confirm that the algorithm you have for generating a random number (larger than 2) is correct. If that never finishes, then we most likely have an algorithm issue since a number being generated that is larger than 2 is much more likely than the number 8 being generated.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 05:46 PM
  2. HELP. Random Number Between
    By Raymond Pittman in forum Java Theory & Questions
    Replies: 3
    Last Post: February 15th, 2011, 09:50 AM
  3. Can a for loop work with random number?
    By humdinger in forum Loops & Control Statements
    Replies: 7
    Last Post: January 10th, 2010, 10:06 PM
  4. my program. Random number generater.
    By james137 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: December 2nd, 2009, 02:58 PM
  5. [SOLVED] Random number method implementation
    By big_c in forum Java Theory & Questions
    Replies: 2
    Last Post: April 15th, 2009, 01:10 PM