Java Utililty Methods ThreadLocalRandom

List of utility methods to do ThreadLocalRandom

Description

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

Method

intgetRandom8()
Generator an random int in 10^7 ~ 10^8
Use nextInt(int origin, int bound) in JDK 1.7
return ThreadLocalRandom.current().nextInt(10_000_000, 99_000_000);
intgetRandomBetween(int min, int max)
get Random Between
Preconditions.checkArgument(max > min);
return ThreadLocalRandom.current().nextInt(max - min) + min;
intgetRandomBoundedInt(int bound)
bound is exclusive
return ThreadLocalRandom.current().nextInt(bound);
doublegetRandomDouble(double a, double b)
get Random Double
if (a == b)
    return a;
return ThreadLocalRandom.current().nextDouble(a, b);
EgetRandomElement(E[] array)
get Random Element
return array[getRandomInt(array.length)];
TgetRandomElement(List elements)
get Random Element
int size = elements.size();
if (size > 0) {
    return elements.get(ThreadLocalRandom.current().nextInt(size));
} else {
    return null;
floatgetRandomFloat()
returns (.nextFloat() * 10 ) * (1/2 chance of *= -1)
float randomFloat = ThreadLocalRandom.current().nextFloat() * 10;
float coinFlip = ThreadLocalRandom.current().nextInt(2);
if (coinFlip > 0) {
    randomFloat *= -1;
return randomFloat;
StringgetRandomId(@Nonnull Random random, int length)
get Random Id
final char[] buf = new char[length];
for (int i = 0; i < length; ++i) {
    buf[i] = RANDOM_ID_CHARS[random.nextInt(RANDOM_ID_CHARS.length)];
return new String(buf);
intgetRandomInt()
get Random Int
return RANDOM.nextInt();
intgetRandomInt(int a, int b)
get Random Int
if (a == b)
    return a;
if (a > b)
    return ThreadLocalRandom.current().nextInt(b, a);
return ThreadLocalRandom.current().nextInt(a, b);