Java ThreadLocalRandom generateStringFromCharacters(final Random random, final char[] validChars, final int min, final int max)

Here you can find the source of generateStringFromCharacters(final Random random, final char[] validChars, final int min, final int max)

Description

Generate a random string from an array of characters

License

Open Source License

Parameter

Parameter Description
validChars to chose from
min length of the string
max length of the string

Declaration

public static String generateStringFromCharacters(final Random random, final char[] validChars, final int min,
        final int max) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

public class Main {
    /**//from  w  w w.  j  av a2  s  .co m
     * Generate a random string from an array of characters
     * 
     * @param validChars to chose from
     * @param min length of the string
     * @param max length of the string
     * @return
     */
    public static String generateStringFromCharacters(final Random random, final char[] validChars, final int min,
            final int max) {
        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);
    }
}

Related

  1. generateRandomDoubleVector(int dimension, double bottomLimit, double topLimit)
  2. generateRandomisedId()
  3. generateRandomSalt(int saltSize)
  4. generateString(int length)
  5. generateString(Random rng, String characters, int length)
  6. genRandomString()
  7. get_a_number_between_min_and_max(int min, int max)
  8. getBoolean()
  9. getElement(boolean[] array)