Class RandomStringUtils


  • @Deprecated(since="2021-04-30")
    public class RandomStringUtils
    extends java.lang.Object
    Deprecated.
    Commons Lang 2 is in maintenance mode. Commons Lang 3 should be used instead.

    Operations for random Strings.

    Currently 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
    • Constructor Summary

      Constructors 
      Constructor Description
      RandomStringUtils()
      Deprecated.
      RandomStringUtils instances should NOT be constructed in standard programming.
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static java.lang.String random​(int count)
      Deprecated.
      Creates a random string whose length is the number of characters specified.
      static java.lang.String random​(int count, boolean letters, boolean numbers)
      Deprecated.
      Creates a random string whose length is the number of characters specified.
      static java.lang.String random​(int count, char[] chars)
      Deprecated.
      Creates a random string whose length is the number of characters specified.
      static java.lang.String random​(int count, int start, int end, boolean letters, boolean numbers)
      Deprecated.
      Creates a random string whose length is the number of characters specified.
      static java.lang.String random​(int count, int start, int end, boolean letters, boolean numbers, char[] chars)
      Deprecated.
      Creates a random string based on a variety of options, using default source of randomness.
      static java.lang.String random​(int count, int start, int end, boolean letters, boolean numbers, char[] chars, java.util.Random random)
      Deprecated.
      Creates a random string based on a variety of options, using supplied source of randomness.
      static java.lang.String random​(int count, java.lang.String chars)
      Deprecated.
      Creates a random string whose length is the number of characters specified.
      static java.lang.String randomAlphabetic​(int count)
      Deprecated.
      Creates a random string whose length is the number of characters specified.
      static java.lang.String randomAlphanumeric​(int count)
      Deprecated.
      Creates a random string whose length is the number of characters specified.
      static java.lang.String randomAscii​(int count)
      Deprecated.
      Creates a random string whose length is the number of characters specified.
      static java.lang.String randomNumeric​(int count)
      Deprecated.
      Creates a random string whose length is the number of characters specified.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • RandomStringUtils

        public RandomStringUtils()
        Deprecated.

        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.

    • Method Detail

      • random

        public static java.lang.String random​(int count)
        Deprecated.

        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
      • randomAscii

        public static java.lang.String randomAscii​(int count)
        Deprecated.

        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
      • randomAlphabetic

        public static java.lang.String randomAlphabetic​(int count)
        Deprecated.

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

        Characters will be chosen from the set of alphabetic characters.

        Parameters:
        count - the length of random string to create
        Returns:
        the random string
      • randomAlphanumeric

        public static java.lang.String randomAlphanumeric​(int count)
        Deprecated.

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

        Characters will be chosen from the set of alpha-numeric characters.

        Parameters:
        count - the length of random string to create
        Returns:
        the random string
      • randomNumeric

        public static java.lang.String randomNumeric​(int count)
        Deprecated.

        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
      • random

        public static java.lang.String random​(int count,
                                              boolean letters,
                                              boolean numbers)
        Deprecated.

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

        Characters will be chosen from the set of alpha-numeric characters as indicated by the arguments.

        Parameters:
        count - the length of random string to create
        letters - if true, generated string will include alphabetic characters
        numbers - if true, generated string will include numeric characters
        Returns:
        the random string
      • random

        public static java.lang.String random​(int count,
                                              int start,
                                              int end,
                                              boolean letters,
                                              boolean numbers)
        Deprecated.

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

        Characters will be chosen from the set of alpha-numeric 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 will include alphabetic characters
        numbers - if true, generated string will include numeric characters
        Returns:
        the random string
      • random

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

        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 - only allow letters?
        numbers - only allow numbers?
        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 - if there are not (end - start) + 1 characters in the set array.
      • random

        public static java.lang.String random​(int count,
                                              int start,
                                              int end,
                                              boolean letters,
                                              boolean numbers,
                                              char[] chars,
                                              java.util.Random random)
        Deprecated.

        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 Integer.MAX_VALUE.

        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
        end - the position in set of chars to end before
        letters - only allow letters?
        numbers - only allow numbers?
        chars - the set of chars to choose randoms from. If null, then it will use the set of all chars.
        random - a source of randomness.
        Returns:
        the random string
        Throws:
        java.lang.ArrayIndexOutOfBoundsException - if there are not (end - start) + 1 characters in the set array.
        java.lang.IllegalArgumentException - if count < 0.
        Since:
        2.0
      • random

        public static java.lang.String random​(int count,
                                              java.lang.String chars)
        Deprecated.

        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 String containing the set of characters to use, may be null
        Returns:
        the random string
        Throws:
        java.lang.IllegalArgumentException - if count < 0.
      • random

        public static java.lang.String random​(int count,
                                              char[] chars)
        Deprecated.

        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 - if count < 0.