Skip to content

Instantly share code, notes, and snippets.

@bison--
Last active February 27, 2020 13:47
Show Gist options
  • Save bison--/cdb11833698e0358ee9f6a6fc067774a to your computer and use it in GitHub Desktop.
Save bison--/cdb11833698e0358ee9f6a6fc067774a to your computer and use it in GitHub Desktop.
Java random helper
import java.util.Random;
public class hRandom {
private static Random localRandom;
/**
* returns a random value between min (inclusive) and max (inclusive)
* @param min minimum value
* @param max maximum value
* @return random integer
*/
public static int getRandomNumberInRange(int min, int max) {
// only create one instance!
if (localRandom == null) {
localRandom = new Random();
}
if (min >= max) {
throw new IllegalArgumentException("max ("+ max +") must be greater than min ("+ min +")");
}
return localRandom.nextInt((max - min) + 1) + min;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment