程序包 cn.fxcraft.util

类 RandomUtil


  • public class RandomUtil
    extends java.lang.Object
    随机数工具类
    作者:
    Germ 2020年05月02日 0时35分
    • 字段概要

      字段 
      修饰符和类型 字段 说明
      static java.lang.String BASE_CHAR
      用于随机选的字符
      static java.lang.String BASE_CHAR_NUMBER
      用于随机选的字符和数字
      static java.lang.String BASE_NUMBER
      用于随机选的数字
    • 方法概要

      所有方法 静态方法 具体方法 
      修饰符和类型 方法 说明
      static java.util.concurrent.ThreadLocalRandom getRandom()
      获取随机数生成器对象
      static boolean randomBoolean()
      获得随机Boolean值
      static char randomChar()
      随机字母或数字,小写
      static char randomChar​(java.lang.String baseString)
      随机字符
      static char randomChinese()
      随机汉字('一'-'鿿')
      static double randomDouble()
      获得随机数[0, 1)
      static double randomDouble​(double limit)
      获得指定范围内的随机数 [0,limit)
      static double randomDouble​(double min, double max)
      获得指定范围内的随机数
      static double randomDouble​(java.lang.String scope)
      从范围中获得随机数(格式 x-y 或者 [x,y])
      static <T> T randomEle​(java.util.List<T> list)
      随机获得列表中的元素
      static <T> T randomEle​(java.util.List<T> list, int limit)
      随机获得列表中的元素
      static <T> T randomEle​(T[] array)
      随机获得数组中的元素
      static <T> T randomEle​(T[] array, int limit)
      随机获得数组中的元素
      static <T> java.util.Set<T> randomEleSet​(java.util.Collection<T> collection, int count)
      随机获得列表中的一定量的不重复元素,返回Set
      static <T> java.util.List<T> randomElse​(java.util.List<T> list, int count)
      随机获得列表中的一定量元素
      static int randomInt​(int limit)
      获得指定范围内的随机数 [0,limit)
      static int randomInt​(int min, int max)
      获得指定范围内的随机数
      static int randomInt​(java.lang.String scope)
      从范围中获得随机数(格式 x-y 或者 [x,y])
      static long randomLong()
      获得随机数
      static long randomLong​(long limit)
      获得指定范围内的随机数 [0,limit)
      static long randomLong​(long min, long max)
      获得指定范围内的随机数[min, max)
      static long randomLong​(java.lang.String scope)
      从范围中获得随机数(格式 x-y 或者 [x,y])
      static char randomNumber()
      随机数字,数字为0~9单个数字
      static java.lang.String randomNumbers​(int length)
      获得一个只包含数字的字符串
      static java.lang.String randomString​(int length)
      获得一个随机的字符串(只包含数字和字符)
      static java.lang.String randomString​(java.lang.String baseString, int length)
      获得一个随机的字符串
      static java.lang.String randomStringUpper​(int length)
      获得一个随机的字符串(只包含数字和大写字符)
      • 从类继承的方法 java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 字段详细资料

      • BASE_NUMBER

        public static final java.lang.String BASE_NUMBER
        用于随机选的数字
        另请参阅:
        常量字段值
      • BASE_CHAR

        public static final java.lang.String BASE_CHAR
        用于随机选的字符
        另请参阅:
        常量字段值
      • BASE_CHAR_NUMBER

        public static final java.lang.String BASE_CHAR_NUMBER
        用于随机选的字符和数字
        另请参阅:
        常量字段值
    • 方法详细资料

      • getRandom

        public static java.util.concurrent.ThreadLocalRandom getRandom()
        获取随机数生成器对象
        返回:
        ThreadLocalRandom
      • randomBoolean

        public static boolean randomBoolean()
        获得随机Boolean值
        返回:
        true or false
        从以下版本开始:
        4.5.9
      • randomChinese

        public static char randomChinese()
        随机汉字('一'-'鿿')
        返回:
        随机的汉字字符
        从以下版本开始:
        5.7.15
      • randomInt

        public static int randomInt​(java.lang.String scope)
        从范围中获得随机数(格式 x-y 或者 [x,y])
        参数:
        scope - 范围(比如 1-10 或者 [1,10])
        返回:
        整数随机数
      • randomInt

        public static int randomInt​(int min,
                                    int max)
        获得指定范围内的随机数
        参数:
        min - 最小数(包含)
        max - 最大数(不包含)
        返回:
        随机数
      • randomInt

        public static int randomInt​(int limit)
        获得指定范围内的随机数 [0,limit)
        参数:
        limit - 限制随机数的范围,不包括这个数
        返回:
        随机数
      • randomLong

        public static long randomLong​(java.lang.String scope)
        从范围中获得随机数(格式 x-y 或者 [x,y])
        参数:
        scope - 范围(比如 1-10 或者 [1,10])
        返回:
        长整数随机数
      • randomLong

        public static long randomLong​(long min,
                                      long max)
        获得指定范围内的随机数[min, max)
        参数:
        min - 最小数(包含)
        max - 最大数(不包含)
        返回:
        随机数
      • randomLong

        public static long randomLong()
        获得随机数
        返回:
        随机数
        从以下版本开始:
        3.3.0
        另请参阅:
        ThreadLocalRandom.nextLong()
      • randomLong

        public static long randomLong​(long limit)
        获得指定范围内的随机数 [0,limit)
        参数:
        limit - 限制随机数的范围,不包括这个数
        返回:
        随机数
      • randomDouble

        public static double randomDouble​(java.lang.String scope)
        从范围中获得随机数(格式 x-y 或者 [x,y])
        参数:
        scope - 范围(比如 1-10 或者 [1,10])
        返回:
        浮点型随机数
      • randomDouble

        public static double randomDouble​(double min,
                                          double max)
        获得指定范围内的随机数
        参数:
        min - 最小数(包含)
        max - 最大数(不包含)
        返回:
        随机数
      • randomDouble

        public static double randomDouble()
        获得随机数[0, 1)
        返回:
        随机数
      • randomDouble

        public static double randomDouble​(double limit)
        获得指定范围内的随机数 [0,limit)
        参数:
        limit - 限制随机数的范围,不包括这个数
        返回:
        随机数
      • randomEle

        public static <T> T randomEle​(java.util.List<T> list)
        随机获得列表中的元素
        类型参数:
        T - 元素类型
        参数:
        list - 列表
        返回:
        随机元素
      • randomEle

        public static <T> T randomEle​(java.util.List<T> list,
                                      int limit)
        随机获得列表中的元素
        类型参数:
        T - 元素类型
        参数:
        list - 列表
        limit - 限制列表的前N项
        返回:
        随机元素
      • randomEle

        public static <T> T randomEle​(T[] array)
        随机获得数组中的元素
        类型参数:
        T - 元素类型
        参数:
        array - 列表
        返回:
        随机元素
        从以下版本开始:
        3.3.0
      • randomEle

        public static <T> T randomEle​(T[] array,
                                      int limit)
        随机获得数组中的元素
        类型参数:
        T - 元素类型
        参数:
        array - 列表
        limit - 限制列表的前N项
        返回:
        随机元素
        从以下版本开始:
        3.3.0
      • randomElse

        public static <T> java.util.List<T> randomElse​(java.util.List<T> list,
                                                       int count)
        随机获得列表中的一定量元素
        类型参数:
        T - 元素类型
        参数:
        list - 列表
        count - 随机取出的个数
        返回:
        随机元素
      • randomEleSet

        public static <T> java.util.Set<T> randomEleSet​(java.util.Collection<T> collection,
                                                        int count)
        随机获得列表中的一定量的不重复元素,返回Set
        类型参数:
        T - 元素类型
        参数:
        collection - 列表
        count - 随机取出的个数
        返回:
        随机元素
      • randomString

        public static java.lang.String randomString​(int length)
        获得一个随机的字符串(只包含数字和字符)
        参数:
        length - 字符串的长度
        返回:
        随机字符串
      • randomStringUpper

        public static java.lang.String randomStringUpper​(int length)
        获得一个随机的字符串(只包含数字和大写字符)
        参数:
        length - 字符串的长度
        返回:
        随机字符串
        从以下版本开始:
        4.0.13
      • randomNumbers

        public static java.lang.String randomNumbers​(int length)
        获得一个只包含数字的字符串
        参数:
        length - 字符串的长度
        返回:
        随机字符串
      • randomString

        public static java.lang.String randomString​(java.lang.String baseString,
                                                    int length)
        获得一个随机的字符串
        参数:
        baseString - 随机字符选取的样本
        length - 字符串的长度
        返回:
        随机字符串
      • randomNumber

        public static char randomNumber()
        随机数字,数字为0~9单个数字
        返回:
        随机数字字符
        从以下版本开始:
        3.1.2
      • randomChar

        public static char randomChar()
        随机字母或数字,小写
        返回:
        随机字符
        从以下版本开始:
        3.1.2
      • randomChar

        public static char randomChar​(java.lang.String baseString)
        随机字符
        参数:
        baseString - 随机字符选取的样本
        返回:
        随机字符
        从以下版本开始:
        3.1.2