random classneed help settin upper and lower limit to a random number
Page 1 of 1
2 Replies - 1050 Views - Last Post: 24 April 2007 - 03:41 PM
#1
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!
Replies To: random class
#2
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.
In your main method, or in whatever class you're using this, you need to create a Random Object:
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:
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.
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.
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
Re: random class
Posted 24 April 2007 - 03:41 PM
thank you, that is exaclty what i was looking for, thanks a lot!
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|