The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
January 2002

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

sol

Posted by hari on January 29, 2002 at 12:30 AM

Hope this method helps you.You can get random number in what ever the range u want using this method.

Random random = new Random();


//Method to generate a random number between -50 and 50
public static int randomInt(int min, int max){
//generate an Integer random number
int result = random.nextInt();
//Logic to get the randum number between min(-50) and max(50) values
if (result < 0)
result = -result;
result %= (max - min + 1);
return result + min;
}




Replies:
  • Success hugo January 29, 2002 at 7:13 AM (1)
    • welcome hari January 30, 2002 at 6:13 AM (0)

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us