Skip to content

Instantly share code, notes, and snippets.

@iezg
Created January 9, 2013 11:21
Show Gist options
  • Save iezg/4492410 to your computer and use it in GitHub Desktop.
Save iezg/4492410 to your computer and use it in GitHub Desktop.
Generating random number in a range with Java
// Example assumes these variables have been initialized
// above, e.g. as method parameters, fields, or otherwise
int min, max;
// nextInt is normally exclusive of the top value,
// so add 1 to make it inclusive
Random rand = new Random();
int randomNum = rand.nextInt(max - min + 1) + min;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment