Last active
February 27, 2020 13:47
-
-
Save bison--/cdb11833698e0358ee9f6a6fc067774a to your computer and use it in GitHub Desktop.
Java random helper
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
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