Skip to content

Instantly share code, notes, and snippets.

@skd1993
Created February 8, 2015 06:03
Show Gist options
  • Save skd1993/0afa98aecbbceefe1416 to your computer and use it in GitHub Desktop.
Save skd1993/0afa98aecbbceefe1416 to your computer and use it in GitHub Desktop.
Random int within a range (java)
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