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

StringRandomString(int length)
Random String
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
StringBuffer buf = new StringBuffer();
for (int i = 0; i < length; i++) {
    int num = ThreadLocalRandom.current().nextInt(62);
    buf.append(str.charAt(num));
return buf.toString();
StringrandomString(int minLength, int maxLength)
random String
Random random = ThreadLocalRandom.current();
return randomString(minLength, maxLength, random);
StringrandomString(List strings)
random String
return strings.get(ThreadLocalRandom.current().nextInt(strings.size()));
String[]randomStringArray(int arrayLength, int stringLength)
Generates a random array of String s with the given arguments.
String[] array = new String[arrayLength];
for (int i = 0; i < array.length; i++) {
    array[i] = random(stringLength);
return array;
intrandomWorld()
Returns a random integer that is a suitable value for both the id and randomNumber properties of a world object.
return 1 + ThreadLocalRandom.current().nextInt(10000);
intrandomWorld()
random World
return 1 + ThreadLocalRandom.current().nextInt(10000);
T[]shuffle(final T[] array)
Shuffle the elements in the given array.
if (array != null && array.length > 0) {
    for (int n = array.length; n > 1;) {
        final int randomIndex = ThreadLocalRandom.current().nextInt(n);
        n--;
        if (n != randomIndex) {
            final T tmp = array[randomIndex];
            array[randomIndex] = array[n];
            array[n] = tmp;
...
voidshuffle(T[] arr)
shuffle
int n = arr.length;
if (n <= 1) {
    return;
for (int i = n - 1; i > 0; --i) {
    swap(arr, i, randomInt(i));
T[]shuffle(T[] array)
An implementation of the Fisher-Yates shuffle algorithm that will shuffle the elements of an T array.
for (int i = array.length - 1; i > 0; i--) {
    int index = ThreadLocalRandom.current().nextInt(i + 1);
    T a = array[index];
    array[index] = array[i];
    array[i] = a;
return array;
int[]shuffleArray(int[] ar)
shuffle Array
Random rnd = ThreadLocalRandom.current();
for (int i = ar.length - 1; i > 0; i--) {
    int index = rnd.nextInt(i + 1);
    int a = ar[index];
    ar[index] = ar[i];
    ar[i] = a;
return ar;
...