File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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.
 
 
subject: random integers....