# random class

Page 1 of 1

## 2 Replies - 1369 Views - Last Post: 24 April 2007 - 03:41 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=27101&amp;s=cb84f7375487d866548305722c209288&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 cap10kid3

Reputation: -1
• Posts: 36
• Joined: 16-August 05

# random class

Posted 23 April 2007 - 09:11 PM

using the random.nextInt() function, i just need help setting an upper and lower limit. just some examples would be extremely helpful, for example a limit of 0 to 10. thanks a lot!
Is This A Good Question/Topic? 0

## Replies To: random class

### #2 keems21

Reputation: 6
• Posts: 185
• Joined: 03-February 07

## Re: random class

Posted 23 April 2007 - 11:04 PM

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.

I hope that this was what you were looking for.

Good luck.

### #3 cap10kid3

Reputation: -1
• Posts: 36
• Joined: 16-August 05

## Re: random class

Posted 24 April 2007 - 03:41 PM

thank you, that is exaclty what i was looking for, thanks a lot!