Top Description Fields Constructors Methods
org.apache.commons.lang3

public Class RandomStringUtils

extends Object
Class Inheritance
Imports
java.security.SecureRandom, .Security, java.util.Random, java.util.concurrent.ThreadLocalRandom, java.util.function.Supplier

Generates random 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#

Since
1.0
See Also
secure(), secureStrong(), insecure(), SecureRandom#SecureRandom(), SecureRandom#getInstanceStrong(), ThreadLocalRandom#current(), RandomUtils

Field Summary

Modifier and TypeField 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<RandomUtils>
private static RandomStringUtils
private static RandomStringUtils
private static final Supplier<RandomUtils>

Constructor Summary

AccessConstructor and Description
public
RandomStringUtils()
Deprecated TODO Make private in 4.0.

RandomStringUtils instances should NOT be constructed in standard programming.

private

Method Summary

Modifier and TypeMethod and Description
public static RandomStringUtils

Returns:

the singleton instance based on ThreadLocalRandom#current().
insecure
()

Gets the singleton instance based on ThreadLocalRandom#current(); which is not cryptographically secure; use secure() to use an algorithms/providers specified in the securerandom.strongAlgorithms Security property.

public String

Returns:

the random string.
next
(final int
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.
next
(final int
the length of random string to create.
count
,
final boolean
if true, generated string may include alphabetic characters.
letters
,
final boolean
if true, generated string may include numeric characters.
numbers
)

Creates a random string whose length is the number of characters specified.

public String

Returns:

the random string.
next
(final int
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.
next
(final int
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 true, generated string may include alphabetic characters.
letters
,
final boolean
if true, generated string may include numeric characters.
numbers
)

Creates a random string whose length is the number of characters specified.

public String

Returns:

the random string.
next
(final int
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 true, generated string may include alphabetic characters.
letters
,
final boolean
if true, generated string may include numeric characters.
numbers
,
final char...
the set of chars to choose randoms from. If null, then it will use the set of all chars.
chars
)

Creates a random string based on a variety of options, using default source of randomness.

public String

Returns:

the random string.
next
(final int
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
)

Creates a random string whose length is the number of characters specified.

public String

Returns:

the random string.
nextAlphabetic
(final int
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.
nextAlphabetic
(final int
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.
nextAlphanumeric
(final int
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.
nextAlphanumeric
(final int
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.
nextAscii
(final int
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.
nextAscii
(final int
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.
nextGraph
(final int
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.
nextGraph
(final int
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.
nextNumeric
(final int
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.
nextNumeric
(final int
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.
nextPrint
(final int
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.
nextPrint
(final int
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.
random
(final int
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.
random
(final int
the length of random string to create.
count
,
final boolean
if true, generated string may include alphabetic characters.
letters
,
final boolean
if true, generated string may include numeric characters.
numbers
)

Creates a random string whose length is the number of characters specified.

public static String

Returns:

the random string.
random
(final int
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 static String

Returns:

the random string.
random
(final int
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 true, generated string may include alphabetic characters.
letters
,
final boolean
if true, generated string may include numeric characters.
numbers
)

Creates a random string whose length is the number of characters specified.

public static String

Returns:

the random string.
random
(final int
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 true, generated string may include alphabetic characters.
letters
,
final boolean
if true, generated string may include numeric characters.
numbers
,
final char...
the set of chars to choose randoms from. If null, then it will use the set of all chars.
chars
)

Creates a random string based on a variety of options, using default source of randomness.

public static String

Returns:

the random string.
random
(int
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 true, generated string may include alphabetic characters.
letters
,
final boolean
if true, generated string may include numeric characters.
numbers
,
final char[]
the set of chars to choose randoms from, must not be empty. If null, then it will use the set of all chars.
chars
,
final Random
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.
random
(final int
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
)

Creates a random string whose length is the number of characters specified.

private Random

Returns:

the Random.
random
()

Gets the Random.

public static String

Returns:

the random string.
randomAlphabetic
(final int
the length of random string to create.
count
)

Creates a random string whose length is the number of characters specified.

public static String

Returns:

the random string.
randomAlphabetic
(final int
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.
randomAlphanumeric
(final int
the length of random string to create.
count
)

Creates a random string whose length is the number of characters specified.

public static String

Returns:

the random string.
randomAlphanumeric
(final int
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.
randomAscii
(final int
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.
randomAscii
(final int
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.
randomGraph
(final int
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.
randomGraph
(final int
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.
randomNumeric
(final int
the length of random string to create.
count
)

Creates a random string whose length is the number of characters specified.

public static String

Returns:

the random string.
randomNumeric
(final int
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.
randomPrint
(final int
the length of random string to create.
count
)

Creates a random string whose length is the number of characters specified.

public static String

Returns:

the random string.
randomPrint
(final int
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.

private RandomUtils

Returns:

the RandomUtils.
randomUtils
()

Gets the RandomUtils.

public static RandomStringUtils

Returns:

the singleton instance based on SecureRandom#SecureRandom().
secure
()

Gets the singleton instance based on SecureRandom#SecureRandom() which uses a secure random number generator (RNG) implementing the default random number algorithm.

public static RandomStringUtils

Returns:

the singleton instance based on SecureRandom#getInstanceStrong().
secureStrong
()

Gets the singleton instance based on SecureRandom#getInstanceStrong() which uses an algorithms/providers specified in the securerandom.strongAlgorithms Security property.

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait