random class

need help settin upper and lower limit to a random number

Page 1 of 1

2 Replies - 1214 Views - Last Post: 24 April 2007 - 03:41 PM Rate Topic: -----

#1 cap10kid3  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 cap10kid3  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • 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!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1