On this page

RandomStringUtils

public class org.apache.commons.lang3.RandomStringUtils[source]

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#

See also:
Since:

1.0

Constructors

RandomStringUtils()

Deprecated. RandomStringUtils instances should NOT be constructed in standard programming.

@Deprecated public RandomStringUtils()[source]

Deprecated: TODO Make private in 4.0.

RandomStringUtils instances should NOT be constructed in standard programming. Instead, the class should be used as RandomStringUtils.random(5);.

This constructor is public to permit tools that require a JavaBean instance to operate.

Methods

insecure()

Gets the singleton instance based on ThreadLocalRandom#current(); which is not cryptographically secure; for more secure processing use #secure() or #secureStrong().

next(int)

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

next(int, boolean, boolean)

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

next(int, char[])

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

next(int, int, int, boolean, boolean)

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

next(int, int, int, boolean, boolean, char[])

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

next(int, String)

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

nextAlphabetic(int)

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

nextAlphabetic(int, int)

Creates a random string whose length is between the inclusive minimum and the exclusive maximum.

nextAlphanumeric(int)

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

nextAlphanumeric(int, int)

Creates a random string whose length is between the inclusive minimum and the exclusive maximum.

nextAscii(int)

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

nextAscii(int, int)

Creates a random string whose length is between the inclusive minimum and the exclusive maximum.

nextGraph(int)

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

nextGraph(int, int)

Creates a random string whose length is between the inclusive minimum and the exclusive maximum.

nextNumeric(int)

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

nextNumeric(int, int)

Creates a random string whose length is between the inclusive minimum and the exclusive maximum.

nextPrint(int)

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

nextPrint(int, int)

Creates a random string whose length is between the inclusive minimum and the exclusive maximum.

random(int)

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

random(int, boolean, boolean)

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

random(int, char[])

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

random(int, int, int, boolean, boolean)

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

random(int, int, int, boolean, boolean, char[])

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

random(int, int, int, boolean, boolean, char[], Random)

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

random(int, String)

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

randomAlphabetic(int)

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

randomAlphabetic(int, int)

Deprecated. Creates a random string whose length is between the inclusive minimum and the exclusive maximum.

randomAlphanumeric(int)

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

randomAlphanumeric(int, int)

Deprecated. Creates a random string whose length is between the inclusive minimum and the exclusive maximum.

randomAscii(int)

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

randomAscii(int, int)

Deprecated. Creates a random string whose length is between the inclusive minimum and the exclusive maximum.

randomGraph(int)

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

randomGraph(int, int)

Deprecated. Creates a random string whose length is between the inclusive minimum and the exclusive maximum.

randomNumeric(int)

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

randomNumeric(int, int)

Deprecated. Creates a random string whose length is between the inclusive minimum and the exclusive maximum.

randomPrint(int)

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

randomPrint(int, int)

Deprecated. Creates a random string whose length is between the inclusive minimum and the exclusive maximum.

secure()

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

secureStrong()

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

toString()

public static RandomStringUtils insecure()[source]

Gets the singleton instance based on ThreadLocalRandom#current(); which is not cryptographically secure; for more secure processing use #secure() or #secureStrong().

The method ThreadLocalRandom#current() is called on-demand.

Returns:

The singleton instance based on ThreadLocalRandom#current().

See also:
Since:

3.16.0

public String next(int count)[source]

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

Characters will be chosen from the set of all characters.

Parameters:

count – The length of random string to create.

Returns:

The random string.

Throws:

java.lang.IllegalArgumentException – Thrown if count < 0.

Since:

3.16.0

public String next(int count, boolean letters, boolean numbers)[source]

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

Characters will be chosen from the set of alphanumeric characters as indicated by the arguments.

Parameters:
  • count – The length of random string to create.

  • letters – if true, generated string may include alphabetic characters.

  • numbers – if true, generated string may include numeric characters.

Returns:

The random string.

Throws:

java.lang.IllegalArgumentException – Thrown if count < 0.

Since:

3.16.0

public String next(int count, char... chars)[source]

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

Characters will be chosen from the set of characters specified.

Parameters:
  • count – The length of random string to create.

  • chars – The character array containing the set of characters to use, may be null.

Returns:

The random string.

Throws:

java.lang.IllegalArgumentException – Thrown if count < 0.

Since:

3.16.0

public String next(int count, int start, int end, boolean letters, boolean numbers)[source]

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

Characters will be chosen from the set of alphanumeric characters as indicated by the arguments.

Parameters:
  • count – The length of random string to create.

  • start – The position in set of chars to start at.

  • end – The position in set of chars to end before.

  • letters – if true, generated string may include alphabetic characters.

  • numbers – if true, generated string may include numeric characters.

Returns:

The random string.

Throws:

java.lang.IllegalArgumentException – Thrown if count < 0.

Since:

3.16.0

public String next(int count, int start, int end, boolean letters, boolean numbers, char... chars)[source]

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

This method has exactly the same semantics as #random(int,int,int,boolean,boolean,char[],Random), but instead of using an externally supplied source of randomness, it uses the internal static Random instance.

Parameters:
  • count – The length of random string to create.

  • start – The position in set of chars to start at.

  • end – The position in set of chars to end before.

  • letters – if true, generated string may include alphabetic characters.

  • numbers – if true, generated string may include numeric characters.

  • chars – The set of chars to choose randoms from. If null, then it will use the set of all chars.

Returns:

The random string.

Throws:
  • java.lang.ArrayIndexOutOfBoundsException – Thrown if there are not (end - start) + 1 characters in the set array.

  • java.lang.IllegalArgumentException – Thrown if count < 0.

public String next(int count, String chars)[source]

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

Characters will be chosen from the set of characters specified by the string, must not be empty. If null, the set of all characters is used.

Parameters:
  • count – The length of random string to create.

  • chars – The String containing the set of characters to use, may be null, but must not be empty.

Returns:

The random string.

Throws:

java.lang.IllegalArgumentException – Thrown if count < 0 or the string is empty.

Since:

3.16.0

public String nextAlphabetic(int count)[source]

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

Characters will be chosen from the set of Latin alphabetic characters (a-z, A-Z).

Parameters:

count – The length of random string to create.

Returns:

The random string.

Throws:

java.lang.IllegalArgumentException – Thrown if count < 0.

public String nextAlphabetic(int minLengthInclusive, int maxLengthExclusive)[source]

Creates a random string whose length is between the inclusive minimum and the exclusive maximum.

Characters will be chosen from the set of Latin alphabetic characters (a-z, A-Z).

Parameters:
  • minLengthInclusive – The inclusive minimum length of the string to generate.

  • maxLengthExclusive – The exclusive maximum length of the string to generate.

Returns:

The random string.

Since:

3.5

public String nextAlphanumeric(int count)[source]

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

Characters will be chosen from the set of Latin alphabetic characters (a-z, A-Z) and the digits 0-9.

Parameters:

count – The length of random string to create.

Returns:

The random string.

Throws:

java.lang.IllegalArgumentException – Thrown if count < 0.

public String nextAlphanumeric(int minLengthInclusive, int maxLengthExclusive)[source]

Creates a random string whose length is between the inclusive minimum and the exclusive maximum.

Characters will be chosen from the set of Latin alphabetic characters (a-z, A-Z) and the digits 0-9.

Parameters:
  • minLengthInclusive – The inclusive minimum length of the string to generate.

  • maxLengthExclusive – The exclusive maximum length of the string to generate.

Returns:

The random string.

Since:

3.5

public String nextAscii(int count)[source]

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

Characters will be chosen from the set of characters whose ASCII value is between 32 and 126 (inclusive).

Parameters:

count – The length of random string to create.

Returns:

The random string.

Throws:

java.lang.IllegalArgumentException – Thrown if count < 0.

public String nextAscii(int minLengthInclusive, int maxLengthExclusive)[source]

Creates a random string whose length is between the inclusive minimum and the exclusive maximum.

Characters will be chosen from the set of characters whose ASCII value is between 32 and 126 (inclusive).

Parameters:
  • minLengthInclusive – The inclusive minimum length of the string to generate.

  • maxLengthExclusive – The exclusive maximum length of the string to generate.

Returns:

The random string.

Since:

3.5

public String nextGraph(int count)[source]

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

Characters will be chosen from the set of characters which match the POSIX [:graph:] regular expression character class. This class contains all visible ASCII characters (i.e. anything except spaces and control characters).

Parameters:

count – The length of random string to create.

Returns:

The random string.

Throws:

java.lang.IllegalArgumentException – Thrown if count < 0.

Since:

3.5

public String nextGraph(int minLengthInclusive, int maxLengthExclusive)[source]

Creates a random string whose length is between the inclusive minimum and the exclusive maximum.

Characters will be chosen from the set of \p{Graph} characters.

Parameters:
  • minLengthInclusive – The inclusive minimum length of the string to generate.

  • maxLengthExclusive – The exclusive maximum length of the string to generate.

Returns:

The random string.

Since:

3.5

public String nextNumeric(int count)[source]

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

Characters will be chosen from the set of numeric characters.

Parameters:

count – The length of random string to create.

Returns:

The random string.

Throws:

java.lang.IllegalArgumentException – Thrown if count < 0.

public String nextNumeric(int minLengthInclusive, int maxLengthExclusive)[source]

Creates a random string whose length is between the inclusive minimum and the exclusive maximum.

Characters will be chosen from the set of \p{Digit} characters.

Parameters:
  • minLengthInclusive – The inclusive minimum length of the string to generate.

  • maxLengthExclusive – The exclusive maximum length of the string to generate.

Returns:

The random string.

Since:

3.5

public String nextPrint(int count)[source]

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

Characters will be chosen from the set of characters which match the POSIX [:print:] regular expression character class. This class includes all visible ASCII characters and spaces (i.e. anything except control characters).

Parameters:

count – The length of random string to create.

Returns:

The random string.

Throws:

java.lang.IllegalArgumentException – Thrown if count < 0.

Since:

3.16.0

public String nextPrint(int minLengthInclusive, int maxLengthExclusive)[source]

Creates a random string whose length is between the inclusive minimum and the exclusive maximum.

Characters will be chosen from the set of \p{Print} characters.

Parameters:
  • minLengthInclusive – The inclusive minimum length of the string to generate.

  • maxLengthExclusive – The exclusive maximum length of the string to generate.

Returns:

The random string.

Since:

3.16.0

@Deprecated public static String random(int count)[source]

Deprecated: Use #next(int) from #secure(), #secureStrong(), or #insecure().

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

Characters will be chosen from the set of all characters.

Parameters:

count – The length of random string to create.

Returns:

The random string.

Throws:

java.lang.IllegalArgumentException – Thrown if count < 0.

@Deprecated public static String random(int count, boolean letters, boolean numbers)[source]

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

Characters will be chosen from the set of alphanumeric characters as indicated by the arguments.

Parameters:
  • count – The length of random string to create.

  • letters – if true, generated string may include alphabetic characters.

  • numbers – if true, generated string may include numeric characters.

Returns:

The random string.

Throws:

java.lang.IllegalArgumentException – Thrown if count < 0.

@Deprecated public static String random(int count, char... chars)[source]

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

Characters will be chosen from the set of characters specified.

Parameters:
  • count – The length of random string to create.

  • chars – The character array containing the set of characters to use, may be null.

Returns:

The random string.

Throws:

java.lang.IllegalArgumentException – Thrown if count < 0.

@Deprecated public static String random(int count, int start, int end, boolean letters, boolean numbers)[source]

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

Characters will be chosen from the set of alphanumeric characters as indicated by the arguments.

Parameters:
  • count – The length of random string to create.

  • start – The position in set of chars to start at.

  • end – The position in set of chars to end before.

  • letters – if true, generated string may include alphabetic characters.

  • numbers – if true, generated string may include numeric characters.

Returns:

The random string.

Throws:

java.lang.IllegalArgumentException – Thrown if count < 0.

@Deprecated public static String random(int count, int start, int end, boolean letters, boolean numbers, char... chars)[source]

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

This method has exactly the same semantics as #random(int,int,int,boolean,boolean,char[],Random), but instead of using an externally supplied source of randomness, it uses the internal static Random instance.

Parameters:
  • count – The length of random string to create.

  • start – The position in set of chars to start at.

  • end – The position in set of chars to end before.

  • letters – if true, generated string may include alphabetic characters.

  • numbers – if true, generated string may include numeric characters.

  • chars – The set of chars to choose randoms from. If null, then it will use the set of all chars.

Returns:

The random string.

Throws:
  • java.lang.ArrayIndexOutOfBoundsException – Thrown if there are not (end - start) + 1 characters in the set array.

  • java.lang.IllegalArgumentException – Thrown if count < 0.

public static String random(int count, int start, int end, boolean letters, boolean digits, char[] chars, Random random)[source]

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

If start and end are both 0, start and end are set to ' ' and 'z', the ASCII printable characters, will be used, unless letters and numbers are both false, in which case, start and end are set to 0 and Character#MAX_CODE_POINT.

If set is not null, characters between start and end are chosen.

This method accepts a user-supplied Random instance to use as a source of randomness. By seeding a single Random instance with a fixed seed and using it for each call, the same random sequence of strings can be generated repeatedly and predictably.

Parameters:
  • count – The length of random string to create.

  • start – The position in set of chars to start at (inclusive).

  • end – The position in set of chars to end before (exclusive).

  • letters – if true, generated string may include alphabetic characters.

  • digits – if true, generated string may include digit characters.

  • chars – The set of chars to choose randoms from, must not be empty. If null, then it will use the set of all chars.

  • random – A source of randomness.

Returns:

The random string.

Throws:
  • java.lang.ArrayIndexOutOfBoundsException – Thrown if there are not (end - start) + 1 characters in the set array.

  • java.lang.IllegalArgumentException – Thrown if count < 0 or the provided chars array is empty.

Since:

2.0

@Deprecated public static String random(int count, String chars)[source]

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

Characters will be chosen from the set of characters specified by the string, must not be empty. If null, the set of all characters is used.

Parameters:
  • count – The length of random string to create.

  • chars – The String containing the set of characters to use, may be null, but must not be empty.

Returns:

The random string.

Throws:

java.lang.IllegalArgumentException – Thrown if count < 0 or the string is empty.

@Deprecated public static String randomAlphabetic(int count)[source]

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

Characters will be chosen from the set of Latin alphabetic characters (a-z, A-Z).

Parameters:

count – The length of random string to create.

Returns:

The random string.

Throws:

java.lang.IllegalArgumentException – Thrown if count < 0.

@Deprecated public static String randomAlphabetic(int minLengthInclusive, int maxLengthExclusive)[source]

Creates a random string whose length is between the inclusive minimum and the exclusive maximum.

Characters will be chosen from the set of Latin alphabetic characters (a-z, A-Z).

Parameters:
  • minLengthInclusive – The inclusive minimum length of the string to generate.

  • maxLengthExclusive – The exclusive maximum length of the string to generate.

Returns:

The random string.

Since:

3.5

@Deprecated public static String randomAlphanumeric(int count)[source]

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

Characters will be chosen from the set of Latin alphabetic characters (a-z, A-Z) and the digits 0-9.

Parameters:

count – The length of random string to create.

Returns:

The random string.

Throws:

java.lang.IllegalArgumentException – Thrown if count < 0.

@Deprecated public static String randomAlphanumeric(int minLengthInclusive, int maxLengthExclusive)[source]

Creates a random string whose length is between the inclusive minimum and the exclusive maximum.

Characters will be chosen from the set of Latin alphabetic characters (a-z, A-Z) and the digits 0-9.

Parameters:
  • minLengthInclusive – The inclusive minimum length of the string to generate.

  • maxLengthExclusive – The exclusive maximum length of the string to generate.

Returns:

The random string.

Since:

3.5

@Deprecated public static String randomAscii(int count)[source]

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

Characters will be chosen from the set of characters whose ASCII value is between 32 and 126 (inclusive).

Parameters:

count – The length of random string to create.

Returns:

The random string.

Throws:

java.lang.IllegalArgumentException – Thrown if count < 0.

@Deprecated public static String randomAscii(int minLengthInclusive, int maxLengthExclusive)[source]

Creates a random string whose length is between the inclusive minimum and the exclusive maximum.

Characters will be chosen from the set of characters whose ASCII value is between 32 and 126 (inclusive).

Parameters:
  • minLengthInclusive – The inclusive minimum length of the string to generate.

  • maxLengthExclusive – The exclusive maximum length of the string to generate.

Returns:

The random string.

Since:

3.5

@Deprecated public static String randomGraph(int count)[source]

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

Characters will be chosen from the set of characters which match the POSIX [:graph:] regular expression character class. This class contains all visible ASCII characters (i.e. anything except spaces and control characters).

Parameters:

count – The length of random string to create.

Returns:

The random string.

Throws:

java.lang.IllegalArgumentException – Thrown if count < 0.

Since:

3.5

@Deprecated public static String randomGraph(int minLengthInclusive, int maxLengthExclusive)[source]

Creates a random string whose length is between the inclusive minimum and the exclusive maximum.

Characters will be chosen from the set of \p{Graph} characters.

Parameters:
  • minLengthInclusive – The inclusive minimum length of the string to generate.

  • maxLengthExclusive – The exclusive maximum length of the string to generate.

Returns:

The random string.

Since:

3.5

@Deprecated public static String randomNumeric(int count)[source]

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

Characters will be chosen from the set of numeric characters.

Parameters:

count – The length of random string to create.

Returns:

The random string.

Throws:

java.lang.IllegalArgumentException – Thrown if count < 0.

@Deprecated public static String randomNumeric(int minLengthInclusive, int maxLengthExclusive)[source]

Creates a random string whose length is between the inclusive minimum and the exclusive maximum.

Characters will be chosen from the set of \p{Digit} characters.

Parameters:
  • minLengthInclusive – The inclusive minimum length of the string to generate.

  • maxLengthExclusive – The exclusive maximum length of the string to generate.

Returns:

The random string.

Since:

3.5

@Deprecated public static String randomPrint(int count)[source]

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

Characters will be chosen from the set of characters which match the POSIX [:print:] regular expression character class. This class includes all visible ASCII characters and spaces (i.e. anything except control characters).

Parameters:

count – The length of random string to create.

Returns:

The random string.

Throws:

java.lang.IllegalArgumentException – Thrown if count < 0.

Since:

3.5

@Deprecated public static String randomPrint(int minLengthInclusive, int maxLengthExclusive)[source]

Creates a random string whose length is between the inclusive minimum and the exclusive maximum.

Characters will be chosen from the set of \p{Print} characters.

Parameters:
  • minLengthInclusive – The inclusive minimum length of the string to generate.

  • maxLengthExclusive – The exclusive maximum length of the string to generate.

Returns:

The random string.

Since:

3.5

public static RandomStringUtils secure()[source]

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

The method SecureRandom#SecureRandom() is called on-demand.

Returns:

The singleton instance based on SecureRandom#SecureRandom().

See also:

SecureRandom#SecureRandom()

Since:

3.16.0

public static RandomStringUtils secureStrong()[source]

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

The method SecureRandom#getInstanceStrong() is called on-demand.

Returns:

The singleton instance based on SecureRandom#getInstanceStrong().

See also:

SecureRandom#getInstanceStrong()

Since:

3.17.0

@Override public String toString()[source]