Java Utililty Methods Random String

List of utility methods to do Random String

Description

The list of methods to do Random String are organized into topic(s).

Method

StringgetRandomStringOfLetters(int length)
Generates a random string of characters.
StringBuilder buf = new StringBuilder();
for (int i = 0; i < length; i++)
    buf.append(nextLetter());
return buf.toString();
StringgetString(int length)
get String
char[] base = new char[length];
Random random = new Random(System.nanoTime());
for (int i = 0; i < base.length; i++) {
    base[i] = digits[random.nextInt(digitsSize)];
return new String(base);
StringgetString(int length)
get String
if (length <= 0) {
    length = 6;
List<String> randomLowerLetterList = Arrays.asList(randomLowerLetter);
List<String> randomUpperLetterList = Arrays.asList(randomUpperLetter);
List<String> randomNumberList = Arrays.asList(randomNumber);
List<String> randomList = new ArrayList<String>();
randomList.addAll(randomLowerLetterList);
...
StringgetString(int length)
get String
StringBuilder sb = new StringBuilder(length);
while (length-- > 0) {
    sb.append(randomChar(LETTERS));
return sb.toString();
StringgetString(int n, int arg[])
get String
StringBuilder res = new StringBuilder();
for (int i = 0; i < n; i++) {
    res.append(getChar(arg));
return res.toString();
StringrandomCharacter(String availableValues)
returns one random character from specified availableValues string
int position = (int) (Math.random() * availableValues.length());
return availableValues.substring(position, position + 1);
StringrandomFileString()
random File String
final double FILECHARS_length = FILECHARS.length();
final int LEN = 6;
final char[] result = new char[LEN];
int index = (int) (Math.random() * 6d);
for (int i = 0; i < LEN; i++) {
    if (index >= LEN) {
        index = 0;
    result[index++] = FILECHARS.charAt((int) (Math.random() * FILECHARS_length));
return new String(result);
StringrandomFixedByteLengthUnicodeString(Random r, int length)
Returns random string, with a given UTF-8 byte length
final char[] buffer = new char[length * 3];
int bytes = length;
int i = 0;
for (; i < buffer.length && bytes != 0; i++) {
    int t;
    if (bytes >= 4) {
        t = r.nextInt(5);
    } else if (bytes >= 3) {
...
voidrandomFixedLengthUnicodeString(Random random, char[] chars, int offset, int length)
Fills provided char[] with valid random unicode code unit sequence.
int i = offset;
final int end = offset + length;
while (i < end) {
    final int t = random.nextInt(5);
    if (0 == t && i < length - 1) {
        chars[i++] = (char) nextInt(random, 0xd800, 0xdbff);
        chars[i++] = (char) nextInt(random, 0xdc00, 0xdfff);
    } else if (t <= 1) {
...
String[]randomize(String[] orderedList)
Returns the input Strings in random order.
ArrayList list = stringArrayToArrayList(orderedList);
Random random = new Random(System.currentTimeMillis());
ArrayList randomList = new ArrayList(orderedList.length);
System.out.println("list.size():" + list.size());
int index;
while (list.size() > 0) {
    index = random.nextInt(list.size());
    randomList.add(list.get(index));
...