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

intgetRandomInt(int limit)
get Random Int
return RANDOM.nextInt(limit);
intgetRandomInt(int n)
get Random Int
return getThreadLocalRandom().nextInt(n);
intgetRandomInt(int r)
Provide random numbers
return ThreadLocalRandom.current().nextInt(r);
IntegergetRandomInteger()
get Random Integer
return ThreadLocalRandom.current().nextInt(1, 1000);
IntegergetRandomIntegerElement(List list)
get Random Integer Element
Integer selectedElement = null;
if (list != null && !list.isEmpty()) {
    int listSize = list.size();
    int randomIndex = ThreadLocalRandom.current().nextInt(0, listSize);
    selectedElement = list.get(randomIndex);
return selectedElement;
ListgetRandomNumbers(Integer startNumber, Integer endNumber, Integer pageSize, Integer numNumbers)
get Random Numbers
Set<Integer> integers = new HashSet<>();
int maxNumbers = (endNumber - startNumber) / pageSize;
if (numNumbers > maxNumbers) {
    numNumbers = maxNumbers;
while (integers.size() < numNumbers) {
    int i = ThreadLocalRandom.current().nextInt(startNumber, endNumber + 1);
    i = (i / pageSize) * pageSize;
...
StringgetRandomPassword()
get Random Password
Random random = ThreadLocalRandom.current();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 5 + random.nextInt(15); i++) {
    char c = chars[random.nextInt(chars.length)];
    sb.append(c);
return sb.toString();
StringgetRandomStr(int len)
get Random Str
StringBuffer sb = new StringBuffer();
for (int i = 0; i < len; i++) {
    int num = ThreadLocalRandom.current().nextInt(10);
    sb.append(num);
return sb.toString();
StringgetRandomString(int length)
get Random String
return getRandomString(length, ThreadLocalRandom.current());
TgetRandomTarget(List targets)
get Random Target
int randomIndex = ThreadLocalRandom.current().nextInt(targets.size());
return targets.get(randomIndex);