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

voidrandomDelay()
random Delay
int delayInMS = ThreadLocalRandom.current().nextInt(MIN_DELAY_IN_MS, MAX_DELAY_IN_MS);
try {
    Thread.sleep(delayInMS);
} catch (InterruptedException e) {
    throw new RuntimeException(e);
doublerandomDouble(final double min, final double max)
Returns a random long at least min, <= max.
if (max <= min) {
    throw new Exception("Invalid max <= min.");
return ThreadLocalRandom.current().nextDouble(min, max);
CollectionrandomElement(Collection c, int n, boolean unique)
Selects pseudo-random elements from a collection
List<E> out = new ArrayList<>();
List<E> l = new ArrayList<>(c);
while (out.size() < n) {
    E e = l.get(ThreadLocalRandom.current().nextInt(0, l.size()));
    out.add(e);
    if (unique)
        l.remove(e);
return out;
TrandomEnum(Class clazz)
random Enum
return clazz.getEnumConstants()[ThreadLocalRandom.current().nextInt(clazz.getEnumConstants().length)];
longrandomFishType()
random Fish Type
return random.nextInt(4);
StringrandomFixedChars(int size, List pool)
random Fixed Chars
int[] indexs = randomIndexs(size, pool.size());
StringBuffer buf = new StringBuffer();
for (int i = 0; i < indexs.length; i++) {
    buf.append(pool.get(indexs[i]));
return buf.toString();
int[]randomIndexs(int count, int bound)
random Indexs
int[] indexs = new int[count];
for (int i = 0; i < count; i++) {
    indexs[i] = random.nextInt(bound);
return indexs;
intrandomInRange(int min, int max)
random In Range
int random = ThreadLocalRandom.current().nextInt(min, max + 1);
return random;
doublerandomInt()
random Int
return ThreadLocalRandom.current().nextInt();
intrandomInt(int bound)
Return randomize integer that is bounded by given bound
return RANDOM.nextInt(bound);