6 Replies Latest reply on Apr 11, 2007 12:21 AM by 807606

# How can you generate a random number?

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?
use java.util.Random
• ###### 2. Re: How can you generate a random number?
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?
Calculate a number between 0 and 400, then add 100.
• ###### 4. Re: How can you generate a random number?
So, like this:
``rand.nextInt(400+100);``
or like this:
``rand.nextInt(400)+100;``
• ###### 5. Re: How can you generate a random number?
I'll take choice number two.
• ###### 6. Re: How can you generate a random number?
If you want 100 and 500 to be valid numbers then you will need this:
``rand.nextInt(401)+100;``