Welcome to Dream.In.Code
Become a Java Expert!

Join 149,477 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,746 people online right now. Registration is fast and FREE... Join Now!




random class

 
Reply to this topicStart new topic

random class, need help settin upper and lower limit to a random number

cap10kid3
23 Apr, 2007 - 08:11 PM
Post #1

New D.I.C Head
*

Joined: 16 Aug, 2005
Posts: 29


My Contributions
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! smile.gif
User is offlineProfile CardPM
+Quote Post

keems21
RE: Random Class
23 Apr, 2007 - 10:04 PM
Post #2

D.I.C Head
Group Icon

Joined: 3 Feb, 2007
Posts: 183



Thanked: 2 times
Dream Kudos: 25
My Contributions
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.
CODE

import java.util.Random;


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

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:

CODE

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.

CODE

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.
User is offlineProfile CardPM
+Quote Post

cap10kid3
RE: Random Class
24 Apr, 2007 - 02:41 PM
Post #3

New D.I.C Head
*

Joined: 16 Aug, 2005
Posts: 29


My Contributions
thank you, that is exaclty what i was looking for, thanks a lot!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 03:13PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month