Class RandomCodeUtils


  • public class RandomCodeUtils
    extends Object
    随机生成工具

    提供纯数字和字母数字混合的随机字符串生成,线程安全

    Author:
    Pan
    • Field Detail

      • DEFAULT_NUMERIC_LENGTH

        public static final int DEFAULT_NUMERIC_LENGTH
        默认纯数字长度
        See Also:
        Constant Field Values
      • DEFAULT_MIXED_LENGTH

        public static final int DEFAULT_MIXED_LENGTH
        默认混合长度
        See Also:
        Constant Field Values
    • Method Detail

      • randomNumeric

        public static String randomNumeric()
        生成默认长度6位纯数字
        Returns:
        String 6位数字字符串
      • randomNumeric

        public static String randomNumeric​(int length)
        生成指定长度的纯数字
        Parameters:
        length - 长度,必须大于0
        Returns:
        String 指定长度的数字字符串
        Throws:
        IllegalArgumentException - 如果 length <= 0
      • randomAlphanumeric

        public static String randomAlphanumeric()
        生成默认长度6位的字母数字混合字符串
        Returns:
        String 6位字母数字混合字符串
      • randomAlphanumeric

        public static String randomAlphanumeric​(int length)
        生成指定长度的字母数字混合字符串
        Parameters:
        length - 长度,必须大于0
        Returns:
        String 指定长度的字母数字混合字符串
        Throws:
        IllegalArgumentException - 如果 length <= 0
      • random

        public static String random​(int length,
                                    String string)
        使用自定义字符集生成随机字符串
        Parameters:
        length - 长度,必须大于0
        string - 字符串
        Returns:
        String 随机字符串
        Throws:
        IllegalArgumentException - 如果 length <= 0 或 charset 为空
      • random

        public static String random​(int length,
                                    char[] charset)
        使用自定义字符集生成随机字符串
        Parameters:
        length - 长度,必须大于0
        charset - 字符集数组,不能为空
        Returns:
        String 随机字符串
        Throws:
        IllegalArgumentException - 如果 length <= 0 或 charset 为空