Strings.
Use secure() to get the singleton instance based on SecureRandom#SecureRandom() which uses a secure random number generator implementing the
default random number algorithm.
Use secureStrong() to get the singleton instance based on SecureRandom#getInstanceStrong() which uses an instance that was selected by using
the algorithms/providers specified in the securerandom.strongAlgorithms Security property.
Use insecure() to get the singleton instance based on ThreadLocalRandom#current() which is not cryptographically secure. In addition,
instances do not use a cryptographically random seed unless the system property java.util.secureRandomSeed is set to
true.
Starting in version 3.17.0, the method secure() uses SecureRandom#SecureRandom() instead of SecureRandom#getInstanceStrong(), and
adds secureStrong().
Starting in version 3.16.0, this class uses secure() for static methods and adds insecure().
Starting in version 3.15.0, this class uses SecureRandom#getInstanceStrong() for static methods.
Before version 3.15.0, this class used ThreadLocalRandom#current() for static methods, which is not cryptographically secure.
RandomStringUtils is intended for simple use cases. For more advanced use cases consider using Apache Commons Text's RandomStringGenerator instead.
The Apache Commons project provides Commons RNG dedicated to pseudo-random number generation, that may be a better choice for applications with more stringent requirements (performance and/or correctness).
Note that private high surrogate characters are ignored. These are Unicode characters that fall between the values 56192 (db80) and 56319 (dbff) as we don't know how to handle them. High and low surrogates are correctly dealt with - that is if a high surrogate is randomly chosen, 55296 (d800) to 56191 (db7f) then it is followed by a low surrogate. If a low surrogate is chosen, 56320 (dc00) to 57343 (dfff) then it is placed after a randomly chosen high surrogate.
#ThreadSafe#
secure(), secureStrong(), insecure(), SecureRandom#SecureRandom(), SecureRandom#getInstanceStrong(), ThreadLocalRandom#current(), RandomUtils
| Modifier and Type | Field and Description |
|---|---|
| private static final char[] | |
| private static final int | |
| private static final int | |
| private static final int | |
| private static final int | |
| private static final int | |
| private static final int | |
| private static final int | |
| private static RandomStringUtils | |
| private final Supplier | |
| private static RandomStringUtils | |
| private static RandomStringUtils | |
| private static final Supplier |
| Access | Constructor and Description |
|---|---|
| public | RandomStringUtils()
Deprecated
TODO Make private in 4.0.
|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static RandomStringUtils | Returns: the singleton instance based onThreadLocalRandom#current().Gets the singleton instance based on |
| public String | Returns: the random string.the length of random string to create. count)Creates a random string whose length is the number of characters specified. |
| public String | Returns: the random string.the length of random string to create. count, final boolean if letters, final boolean true, generated string may include alphabetic characters.if numbers)true, generated string may include numeric characters.Creates a random string whose length is the number of characters specified. |
| public String | Returns: the random string.the length of random string to create. count, final char... the character array containing the set of characters to use, may be null. chars)Creates a random string whose length is the number of characters specified. |
| public String | Returns: the random string.the length of random string to create. count, final int the position in set of chars to start at. start, final int the position in set of chars to end before. end, final boolean if letters, final boolean true, generated string may include alphabetic characters.if numbers)true, generated string may include numeric characters.Creates a random string whose length is the number of characters specified. |
| public String | Returns: the random string.the length of random string to create. count, final int the position in set of chars to start at. start, final int the position in set of chars to end before. end, final boolean if letters, final boolean true, generated string may include alphabetic characters.if numbers, final char... true, generated string may include numeric characters.the set of chars to choose randoms from. If chars)null, then it will use the set of all chars.Creates a random string based on a variety of options, using default source of randomness. |
| public String | |
| public String | Returns: the random string.the length of random string to create. count)Creates a random string whose length is the number of characters specified. |
| public String | Returns: the random string.the inclusive minimum length of the string to generate. minLengthInclusive, final int the exclusive maximum length of the string to generate. maxLengthExclusive)Creates a random string whose length is between the inclusive minimum and the exclusive maximum. |
| public String | Returns: the random string.the length of random string to create. count)Creates a random string whose length is the number of characters specified. |
| public String | Returns: the random string.the inclusive minimum length of the string to generate. minLengthInclusive, final int the exclusive maximum length of the string to generate. maxLengthExclusive)Creates a random string whose length is between the inclusive minimum and the exclusive maximum. |
| public String | Returns: the random string.the length of random string to create. count)Creates a random string whose length is the number of characters specified. |
| public String | Returns: the random string.the inclusive minimum length of the string to generate. minLengthInclusive, final int the exclusive maximum length of the string to generate. maxLengthExclusive)Creates a random string whose length is between the inclusive minimum and the exclusive maximum. |
| public String | Returns: the random string.the length of random string to create. count)Creates a random string whose length is the number of characters specified. |
| public String | Returns: the random string.the inclusive minimum length of the string to generate. minLengthInclusive, final int the exclusive maximum length of the string to generate. maxLengthExclusive)Creates a random string whose length is between the inclusive minimum and the exclusive maximum. |
| public String | Returns: the random string.the length of random string to create. count)Creates a random string whose length is the number of characters specified. |
| public String | Returns: the random string.the inclusive minimum length of the string to generate. minLengthInclusive, final int the exclusive maximum length of the string to generate. maxLengthExclusive)Creates a random string whose length is between the inclusive minimum and the exclusive maximum. |
| public String | Returns: the random string.the length of random string to create. count)Creates a random string whose length is the number of characters specified. |
| public String | Returns: the random string.the inclusive minimum length of the string to generate. minLengthInclusive, final int the exclusive maximum length of the string to generate. maxLengthExclusive)Creates a random string whose length is between the inclusive minimum and the exclusive maximum. |
| public static String | Returns: the random string.the length of random string to create. count)
Deprecated
Use
next(int) from secure(), secureStrong(), or insecure().
Creates a random string whose length is the number of characters specified. |
| public static String | Returns: the random string.the length of random string to create. count, final boolean if letters, final boolean true, generated string may include alphabetic characters.if numbers)true, generated string may include numeric characters.
Deprecated
Use
next(int, boolean, boolean) from secure(), secureStrong(), or insecure().
Creates a random string whose length is the number of characters specified. |
| public static String | Returns: the random string.the length of random string to create. count, final char... the character array containing the set of characters to use, may be null. chars)
Deprecated
Use
next(int, char. from secure(), secureStrong(), or insecure().
Creates a random string whose length is the number of characters specified. |
| public static String | Returns: the random string.the length of random string to create. count, final int the position in set of chars to start at. start, final int the position in set of chars to end before. end, final boolean if letters, final boolean true, generated string may include alphabetic characters.if numbers)true, generated string may include numeric characters.
Deprecated
Use
next(int, int, int, boolean, boolean) from secure(), secureStrong(), or insecure().
Creates a random string whose length is the number of characters specified. |
| public static String | Returns: the random string.the length of random string to create. count, final int the position in set of chars to start at. start, final int the position in set of chars to end before. end, final boolean if letters, final boolean true, generated string may include alphabetic characters.if numbers, final char... true, generated string may include numeric characters.the set of chars to choose randoms from. If chars)null, then it will use the set of all chars.
Deprecated
Use
next(int, int, int, boolean, boolean, char. from secure(), secureStrong(), or insecure().
Creates a random string based on a variety of options, using default source of randomness. |
| public static String | Returns: the random string.the length of random string to create. count, int the position in set of chars to start at (inclusive). start, int the position in set of chars to end before (exclusive). end, final boolean if letters, final boolean true, generated string may include alphabetic characters.if numbers, final char[] true, generated string may include numeric characters.the set of chars to choose randoms from, must not be empty. If chars, final Random null, then it will use the
set of all chars.a source of randomness. random)Creates a random string based on a variety of options, using supplied source of randomness. |
| public static String | Returns: the random string.the length of random string to create. count, final String the String containing the set of characters to use, may be null, but must not be empty. chars)
Deprecated
Use
next(int, String) from secure(), secureStrong(), or insecure().
Creates a random string whose length is the number of characters specified. |
| private Random | |
| public static String | Returns: the random string.the length of random string to create. count)
Deprecated
Use
nextAlphabetic(int) from secure(), secureStrong(), or insecure().
Creates a random string whose length is the number of characters specified. |
| public static String | Returns: the random string.the inclusive minimum length of the string to generate. minLengthInclusive, final int the exclusive maximum length of the string to generate. maxLengthExclusive)
Deprecated
Use
nextAlphabetic(int, int) from secure(), secureStrong(), or insecure().
Creates a random string whose length is between the inclusive minimum and the exclusive maximum. |
| public static String | Returns: the random string.the length of random string to create. count)
Deprecated
Use
nextAlphanumeric(int) from secure(), secureStrong(), or insecure().
Creates a random string whose length is the number of characters specified. |
| public static String | Returns: the random string.the inclusive minimum length of the string to generate. minLengthInclusive, final int the exclusive maximum length of the string to generate. maxLengthExclusive)
Deprecated
Use
nextAlphanumeric(int, int) from secure(), secureStrong(), or insecure().
Creates a random string whose length is between the inclusive minimum and the exclusive maximum. |
| public static String | Returns: the random string.the length of random string to create. count)
Deprecated
Use
nextAscii(int) from secure(), secureStrong(), or insecure().
Creates a random string whose length is the number of characters specified. |
| public static String | Returns: the random string.the inclusive minimum length of the string to generate. minLengthInclusive, final int the exclusive maximum length of the string to generate. maxLengthExclusive)
Deprecated
Use
nextAscii(int, int) from secure(), secureStrong(), or insecure().
Creates a random string whose length is between the inclusive minimum and the exclusive maximum. |
| public static String | Returns: the random string.the length of random string to create. count)
Deprecated
Use
nextGraph(int) from secure(), secureStrong(), or insecure().
Creates a random string whose length is the number of characters specified. |
| public static String | Returns: the random string.the inclusive minimum length of the string to generate. minLengthInclusive, final int the exclusive maximum length of the string to generate. maxLengthExclusive)
Deprecated
Use
nextGraph(int, int) from secure(), secureStrong(), or insecure().
Creates a random string whose length is between the inclusive minimum and the exclusive maximum. |
| public static String | Returns: the random string.the length of random string to create. count)
Deprecated
Use
nextNumeric(int) from secure(), secureStrong(), or insecure().
Creates a random string whose length is the number of characters specified. |
| public static String | Returns: the random string.the inclusive minimum length of the string to generate. minLengthInclusive, final int the exclusive maximum length of the string to generate. maxLengthExclusive)
Deprecated
Use
nextNumeric(int, int) from secure(), secureStrong(), or insecure().
Creates a random string whose length is between the inclusive minimum and the exclusive maximum. |
| public static String | Returns: the random string.the length of random string to create. count)
Deprecated
Use
randomPrint(int) from secure(), secureStrong(), or insecure().
Creates a random string whose length is the number of characters specified. |
| public static String | Returns: the random string.the inclusive minimum length of the string to generate. minLengthInclusive, final int the exclusive maximum length of the string to generate. maxLengthExclusive)
Deprecated
Use
randomPrint(int, int) from secure(), secureStrong(), or insecure().
Creates a random string whose length is between the inclusive minimum and the exclusive maximum. |
| private RandomUtils | |
| public static RandomStringUtils | Returns: the singleton instance based onSecureRandom#SecureRandom().Gets the singleton instance based on |
| public static RandomStringUtils | Returns: the singleton instance based onSecureRandom#getInstanceStrong().Gets the singleton instance based on |
| public String |