Java Utililty Methods Random Int

List of utility methods to do Random Int

Description

The list of methods to do Random Int are organized into topic(s).

Method

intgetRandomInt(int bound)
get a random number
Random random = new Random();
return random.nextInt(bound);
intgetRandomInt(int intStart, int intEnd)
get Random Int
return (int) ((Math.random() * intEnd) + intStart);
intgetRandomInt(int lower, int upper)
get Random Int
int num = (int) Math.floor(rand.nextDouble() * ((upper + 1) - lower));
if (num + lower > upper || num + lower < lower) {
    System.out.println("ERROR: Random returned value of of range!");
    System.exit(1);
return num + lower;
intgetRandomInt(int lower, int upper)
This method is taken from OZONE's BenchmarkImpl class
if (randomGenerator == null) {
    randomGenerator = new Random();
    randomGenerator.setSeed(1L);
int value;
do {
    value = randomGenerator.nextInt();
    value %= upper;
...
intgetRandomInt(int max)
Generates next random Int
Random rand = new Random(System.nanoTime()); 
if (max > 0) {
    return rand.nextInt(max);
return rand.nextInt();
intgetRandomInt(int max)
Get a random int within [0, max) range.
return RANDOM.nextInt(max);
intgetRandomInt(int max)
get Random Int
if (randomGenerator.get() == null) {
    randomGenerator.set(new Random());
return randomGenerator.get().nextInt(max);
intgetRandomInt(int min, int max)
get Random Int
Random rand = new Random();
int x = rand.nextInt(max - min + 1) + min;
x = rand.nextInt(max - min + 1) + min;
return x;
intgetRandomInt(int min, int max)
get Random Int
if (min > max)
    throw new IllegalArgumentException("min > max");
if (min == Integer.MIN_VALUE && max == Integer.MAX_VALUE) { 
    return random.nextInt();
} else {
    return random.nextInt(max - min + 1) + min;
intgetRandomInt(int min, int max, Random random)
Get a random int value in given range
int diff = max - min + 1;
int randomInt = random.nextInt(diff);
return randomInt + min;