Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

random integers....

 
Brandi Love
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try doing % 36 on your random number and adding 25 to it.
Angel
 
Wayne L Johnson
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic