Java ThreadLocalRandom random(char lower, char upper, int length)

Here you can find the source of random(char lower, char upper, int length)

Description

Generates a pseudorandom String object.

License

Open Source License

Parameter

Parameter Description
lower Lower bound, inclusive.
upper Upper bound, inclusive.
length The length of the String to generate.

Return

A random String object with the given length.

Declaration

public static String random(char lower, char upper, int length) 

Method Source Code


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

import java.util.concurrent.ThreadLocalRandom;

public class Main {
    /**/* w  w w.java2  s. co  m*/
     * 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 
     * {@link Character#MIN_VALUE} ({@code 0}), inclusive, to 
     * {@link Character#MAX_VALUE} ({@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
     * (inclusive) to the given upper bound (inclusive).
     * 
     * @param lower The lower bound, inclusive.
     * @param upper The Upper bound, inclusive.
     * @return A random {@code char} value.
     */
    public static char randomChar(int lower, int upper) {
        return (char) ThreadLocalRandom.current().nextInt(lower, upper + 1);
    }
}

Related

  1. rand(final Collection items)
  2. randInt(int min, int max)
  3. random()
  4. random()
  5. random()
  6. random(double min, double max)
  7. random(int min, int max)
  8. random(T[] array)
  9. randomAge()