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

# Thread: HELP. Random Number Between

1. ## 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. ## 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. ## Re: HELP. Random Number Between

Originally Posted by copeg
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. ## Re: HELP. Random Number Between

Originally Posted by Raymond Pittman
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)?