File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

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.
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399

"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.
I agree. Here's the link:
subject: random integers....
It's not a secret anymore!