This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes random integers.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "random integers...." Watch "random integers...." New topic
Author

random integers....

Brandi Love
Ranch Hand

Joined: Sep 19, 2003
Posts: 133
I need to generate a random integer between 25 and 60 for a game I'm writing. I used a while loop to check randomly generated numbers untill I came up with one, but it seems like there must be an easier way to do this. Anyone have any if there is?
Angel Dobbs-Sciortino
Ranch Hand

Joined: Sep 10, 2003
Posts: 101
Try doing % 36 on your random number and adding 25 to it.
Angel
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
Try:

"Math.random()" will return random number between 0.0 and 1.0, exclusive. Multiplying by 36 will give you a number between 0.0 and 36.0. The cast turns that into an "int" between 0 and 35. Adding 25 to that will give you a value between 25 and 60, inclusive.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: random integers....
 
Similar Threads
how to generate random unique alphanumeric numbers
WA #1.....word association
checking load of include files
NEed Help!
java graphic problem