Java ThreadLocalRandom genRandomString()

Here you can find the source of genRandomString()

Description

gen Random String

License

Apache License

Declaration

public static String genRandomString() 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Arrays;

import java.util.List;

import java.util.concurrent.ThreadLocalRandom;

public class Main {
    public static String genRandomString() {
        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()))));
        }/*from   www  . j a  v  a2s  .c o m*/

        return sb.toString();
    }

    public static int getRandomInt(int n) {
        return getThreadLocalRandom().nextInt(n);
    }

    public static ThreadLocalRandom getThreadLocalRandom() {
        return ThreadLocalRandom.current();
    }
}

Related

  1. generateRandomisedId()
  2. generateRandomSalt(int saltSize)
  3. generateString(int length)
  4. generateString(Random rng, String characters, int length)
  5. generateStringFromCharacters(final Random random, final char[] validChars, final int min, final int max)
  6. get_a_number_between_min_and_max(int min, int max)
  7. getBoolean()
  8. getElement(boolean[] array)
  9. getInt(int bound)