Created
February 8, 2015 06:03
-
-
Save skd1993/0afa98aecbbceefe1416 to your computer and use it in GitHub Desktop.
Random int within a range (java)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int randInt(int min, int max) { | |
// NOTE: Usually this should be a field rather than a method | |
// variable so that it is not re-seeded every call. | |
Random rand = new Random(); | |
// nextInt is normally exclusive of the top value, | |
// so add 1 to make it inclusive | |
int randomNum = rand.nextInt((max - min) + 1) + min; | |
return randomNum; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment