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

StringgenerateString(Random rng, String characters, int length)
generate String
char[] text = new char[length];
for (int i = 0; i < length; i++) {
    text[i] = characters.charAt(rng.nextInt(characters.length()));
return new String(text);
StringgenerateStringFromCharacters(final Random random, final char[] validChars, final int min, final int max)
Generate a random string from an array of characters
final int size = ThreadLocalRandom.current().nextInt(min, max + 1);
final char[] localPart = new char[size];
for (int i = 0; i < size; i++) {
    localPart[i] = validChars[random.nextInt(validChars.length - 1)];
return new String(localPart);
StringgenRandomString()
gen Random String
int len = getRandomInt(23) + 1;
List<Integer> asciiBase = Arrays.asList(65, 97);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < len; i++) {
    int randChar = getRandomInt(26);
    sb.append(Character.toChars(randChar + asciiBase.get(getRandomInt(asciiBase.size()))));
return sb.toString();
...
intget_a_number_between_min_and_max(int min, int max)
genumbebetweemianmax
return java.util.concurrent.ThreadLocalRandom.current().nextInt(min, max + 1);
booleangetBoolean()
get Boolean
return ThreadLocalRandom.current().nextBoolean();
booleangetElement(boolean[] array)
get Element
return array[getInt(array.length)];
intgetInt(int bound)
get Int
return ThreadLocalRandom.current().nextInt(bound);
StringgetLetter()
get Letter
String[] let = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r",
        "s", "t", "u", "v", "w", "x" + "y" + "z" };
letter = let[ThreadLocalRandom.current().nextInt(let.length)];
return letter;
ThreadLocalRandomgetRandom()
get Random
return ThreadLocalRandom.current();
intgetRandom(int p)
Generator an random int by 10^(p-1) Use nextInt(int origin, int bound) in JDK 1.7
if (p <= 0 && p > 9)
    return -1;
return ThreadLocalRandom.current().nextInt((int) Math.pow(10, p - 1), (int) Math.pow(10, p) - 1);