Java ThreadLocalRandom randomStringArray(int arrayLength, int stringLength)

Here you can find the source of randomStringArray(int arrayLength, int stringLength)

Description

Generates a random array of String s with the given arguments.

License

Open Source License

Parameter

Parameter Description
arrayLength The length of the array to generate.
stringLength The length of the String elements to populate the array.

Return

A generated array of String objects with the specified arguments.

Declaration

public static String[] randomStringArray(int arrayLength, int stringLength) 

Method Source Code

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

import java.util.concurrent.ThreadLocalRandom;

public class Main {
    /**//  w ww  . ja v a 2s  .c  om
     * Generates a random array of {@code String}s with the given arguments.
     * 
     * @param arrayLength The length of the array to generate.
     * @param stringLength The length of the {@code String} elements to 
     *        populate the array.
     * @return A generated array of {@code String} objects with the 
     *         specified arguments.
     * @see #random(int) 
     */
    public static String[] randomStringArray(int arrayLength, int stringLength) {
        String[] array = new String[arrayLength];
        for (int i = 0; i < array.length; i++) {
            array[i] = random(stringLength);
        }
        return array;
    }

    /**
     * Generates a pseudorandom {@code String} object. May include {@code char}
     * values from {@code 0} to {@code 65535} inclusive.
     * 
     * @param length The length of the {@code String} to generate.
     * @return A random {@code String} object with the given length.
     * @see #randomChar()
     */
    public static String random(int length) {
        return random(Character.MIN_VALUE, Character.MAX_VALUE, length);
    }

    /**
     * Generates a pseudorandom {@code String} object. May include {@code char}
     * values from {@code lower} to {@code upper} inclusive.
     * 
     * @param lower Lower bound, inclusive.
     * @param upper Upper bound, inclusive.
     * @param length The length of the {@code String} to generate.
     * @return A random {@code String} object with the given length.
     * @see #randomChar()
     */
    public static String random(char lower, char upper, int length) {
        if (length <= 0) {
            throw new IllegalArgumentException("length : " + length + " < 0 !");
        }
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            sb.append(randomChar(lower, upper));
        }
        return sb.toString();
    }

    /**
     * Generates a pseudorandom {@code char} value, from {@code 0} to 
     * {@code 65535} inclusive.
     * 
     * @return A random {@code char}.
     */
    public static char randomChar() {
        return randomChar(Character.MIN_VALUE, Character.MAX_VALUE);
    }

    /**
     * Generates a pseudorandom {@code char} value, from the given lower bound
     * to the given upper bound, inclusive.
     * 
     * @param lower Lower bound to generate character.
     * @param upper Upper bound to generate character.
     * @return A random {@code char}.
     */
    public static char randomChar(int lower, int upper) {
        return (char) ThreadLocalRandom.current().nextInt(lower, upper + 1);
    }
}

Related

  1. randomSeed()
  2. randomString()
  3. RandomString(int length)
  4. randomString(int minLength, int maxLength)
  5. randomString(List strings)
  6. randomWorld()
  7. randomWorld()
  8. shuffle(final T[] array)
  9. shuffle(T[] arr)