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 4 of 4

Thread: HELP. Random Number Between

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

    Lightbulb HELP. Random Number Between

    Hi my current code is

    int randomNumber = random.nextInt(899) + 100;


    My teacher said: Also, the formula for the random number will yield a number in the range of
    100 to 998 vs. 100 to 999. (off-by-one error).

    So I need to make it so it puts it between 100 - 999. But, I'm confused on how that works exactly.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,343
    Thanks
    182
    Thanked 831 Times in 774 Posts
    Blog Entries
    5

    Default Re: HELP. Random Number Between

    Break apart the equation and have a look at the API for the Random.nextInt function you use - it returns a value between (and including) 0 and the parameter value (excluding that value). So, doing the math, how would you change this beginning part of the equation to return what you want?

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

    Default Re: HELP. Random Number Between

    Quote Originally Posted by copeg View Post
    Break apart the equation and have a look at the API for the Random.nextInt function you use - it returns a value between (and including) 0 and the parameter value (excluding that value). So, doing the math, how would you change this beginning part of the equation to return what you want?

    Remove the next part?

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,343
    Thanks
    182
    Thanked 831 Times in 774 Posts
    Blog Entries
    5

    Default Re: HELP. Random Number Between

    Quote Originally Posted by Raymond Pittman View Post
    Remove the next part?
    I'm not sure what this means....but if you have a function that you feed a number N and it returns a value between and including 0-N, how would you get it to return a value between 0-(N+1)?

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, 04:46 PM
  2. random
    By b109 in forum Java Theory & Questions
    Replies: 2
    Last Post: June 3rd, 2010, 04:19 AM
  3. Can a for loop work with random number?
    By humdinger in forum Loops & Control Statements
    Replies: 7
    Last Post: January 10th, 2010, 09: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, 01: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