Replies To: random class

Re: random class

I'm probably going to get myself into trouble with this one, but let me give it a shot. It's been a while since I've used random numbers in Java.

So, the first thing you need to do is import the right package. Do this before your programs starts.

import java.util.Random;

In your main method, or in whatever class you're using this, you need to create a Random Object:

Random gen = new Random();

That should work fine. Now, you talk about the nextInt() method. From what I remember, this takes one int as a arguement, that determines the range of the random number created. So, using the newly created Random object:

int randomNum = gen.nextInt(10);

This will give you a random number between 0 and 9 (I'm pretty sure). Say you want a random number between 10 and 19. Then the only thing you need to do is add 10 to a random number between 0 and 9.

int randomNum = gen.nextInt(10) + 10;

This goes for any offset that you want to put in the random number that you create.