This discussion is archived
6 Replies Latest reply: Apr 10, 2007 5:21 PM by 807606

# How can you generate a random number?

Currently Being Moderated
Say I have a timer, and I need it to choose a random number between 100 and 1000. How would I do that?
``     timer.delay(***RANDOM***);``
Thanks!
• ###### 1. Re: How can you generate a random number?
Currently Being Moderated
use java.util.Random
• ###### 2. Re: How can you generate a random number?
Currently Being Moderated
I have this, and it's working for me:

int j = rand.nextInt(500);

But I actually want it to generate a random number between 100 and 500, not 0 and 500. How could I do this?

~Dac
• ###### 3. Re: How can you generate a random number?
Currently Being Moderated
Calculate a number between 0 and 400, then add 100.
• ###### 4. Re: How can you generate a random number?
Currently Being Moderated
So, like this:
``rand.nextInt(400+100);``
or like this:
``rand.nextInt(400)+100;``
• ###### 5. Re: How can you generate a random number?
Currently Being Moderated
I'll take choice number two.
• ###### 6. Re: How can you generate a random number?
Currently Being Moderated
If you want 100 and 500 to be valid numbers then you will need this:
``rand.nextInt(401)+100;``