Class RandomKit

java.lang.Object
org.aoju.bus.core.toolkit.RandomKit

public class RandomKit extends Object
随机工具类
Since:
Java 17+
Author:
Kimi Liu
  • Constructor Details

    • RandomKit

      public RandomKit()
  • Method Details

    • getRandom

      public static ThreadLocalRandom getRandom()
      获取随机数生成器对象 ThreadLocalRandom是JDK 7之后提供并发产生随机数,能够解决多个线程发生的竞争争夺
      Returns:
      ThreadLocalRandom
    • getRandom

      public static Random getRandom(boolean isSecure)
      获取随机数产生器
      Parameters:
      isSecure - 是否为强随机数生成器 (RNG)
      Returns:
      Random
      See Also:
    • getSecureRandom

      public static SecureRandom getSecureRandom()
      获取SecureRandom,类提供加密的强随机数生成器 (RNG)
      Returns:
      SecureRandom
    • getSecureRandom

      public static SecureRandom getSecureRandom(byte[] seed)
      获取SHA1PRNG的SecureRandom,类提供加密的强随机数生成器 (RNG) 注意:此方法获取的是伪随机序列发生器PRNG(pseudo-random number generator)
      Parameters:
      seed - 随机数种子
      Returns:
      SecureRandom
    • randomInt

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

      public static int randomInt()
      获得随机数[0, 1)
      Returns:
      随机数
    • randomInt

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

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

      public static long randomLong()
      获得随机数
      Returns:
      随机数
    • randomLong

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

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

      public static double randomDouble(double min, double max, int scale, RoundingMode roundingMode)
      获得指定范围内的随机数
      Parameters:
      min - 最小数(包含)
      max - 最大数(不包含)
      scale - 保留小数位数
      roundingMode - 保留小数的模式 RoundingMode
      Returns:
      随机数
    • randomDouble

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

      public static double randomDouble(int scale, RoundingMode roundingMode)
      获得指定范围内的随机数
      Parameters:
      scale - 保留小数位数
      roundingMode - 保留小数的模式 RoundingMode
      Returns:
      随机数
    • randomDouble

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

      public static double randomDouble(double limit, int scale, RoundingMode roundingMode)
      获得指定范围内的随机数
      Parameters:
      limit - 限制随机数的范围,不包括这个数
      scale - 保留小数位数
      roundingMode - 保留小数的模式 RoundingMode
      Returns:
      随机数
    • randomBigDecimal

      public static BigDecimal randomBigDecimal()
      获得指定范围内的随机数[0, 1)
      Returns:
      随机数
    • randomBigDecimal

      public static BigDecimal randomBigDecimal(BigDecimal limit)
      获得指定范围内的随机数 [0,limit)
      Parameters:
      limit - 最大数(不包含)
      Returns:
      随机数
    • randomBigDecimal

      public static BigDecimal randomBigDecimal(BigDecimal min, BigDecimal max)
      获得指定范围内的随机数
      Parameters:
      min - 最小数(包含)
      max - 最大数(不包含)
      Returns:
      随机数
    • randomBytes

      public static byte[] randomBytes(int length)
      随机bytes
      Parameters:
      length - 长度
      Returns:
      bytes
    • randomEle

      public static <T> T randomEle(List<T> list)
      随机获得列表中的元素
      Type Parameters:
      T - 元素类型
      Parameters:
      list - 列表
      Returns:
      随机元素
    • randomEle

      public static <T> T randomEle(List<T> list, int limit)
      随机获得列表中的元素
      Type Parameters:
      T - 元素类型
      Parameters:
      list - 列表
      limit - 限制列表的前N项
      Returns:
      随机元素
    • randomEle

      public static <T> T randomEle(T[] array)
      随机获得数组中的元素
      Type Parameters:
      T - 元素类型
      Parameters:
      array - 列表
      Returns:
      随机元素
    • randomEle

      public static <T> T randomEle(T[] array, int limit)
      随机获得数组中的元素
      Type Parameters:
      T - 元素类型
      Parameters:
      array - 列表
      limit - 限制列表的前N项
      Returns:
      随机元素
    • randomEles

      public static <T> List<T> randomEles(List<T> list, int count)
      随机获得列表中的一定量元素
      Type Parameters:
      T - 元素类型
      Parameters:
      list - 列表
      count - 随机取出的个数
      Returns:
      随机元素
    • randomEleList

      public static <T> List<T> randomEleList(List<T> source, int count)
      随机获得列表中的一定量的元素,返回List
      Type Parameters:
      T - 元素类型
      Parameters:
      source - 列表
      count - 随机取出的个数
      Returns:
      随机列表
    • randomEleSet

      public static <T> Set<T> randomEleSet(Collection<T> collection, int count)
      随机获得列表中的一定量的不重复元素,返回Set
      Type Parameters:
      T - 元素类型
      Parameters:
      collection - 列表
      count - 随机取出的个数
      Returns:
      随机元素
      Throws:
      IllegalArgumentException - 需要的长度大于给定集合非重复总数
    • randomString

      public static String randomString(int length)
      获得一个随机的字符串(只包含数字和字符)
      Parameters:
      length - 字符串的长度
      Returns:
      随机字符串
    • randomString

      public static String randomString(String text, int length)
      获得一个随机的字符串
      Parameters:
      text - 随机字符选取的样本
      length - 字符串的长度
      Returns:
      随机字符串
    • randomStringUpper

      public static String randomStringUpper(int length)
      获得一个随机的字符串(只包含数字和大写字符)
      Parameters:
      length - 字符串的长度
      Returns:
      随机字符串
    • randomStringWithout

      public static String randomStringWithout(int length, String elemData)
      获得一个随机的字符串(只包含数字和小写字母) 并排除指定字符串
      Parameters:
      length - 字符串的长度
      elemData - 要排除的字符串,如:去重容易混淆的字符串,oO0、lL1、q9Q、pP,不区分大小写
      Returns:
      随机字符串
    • randomInts

      public static int[] randomInts(int length)
      创建指定长度的随机索引
      Parameters:
      length - 长度
      Returns:
      随机索引
    • randomNumbers

      public static String randomNumbers(int length)
      获得一个只包含数字的字符串
      Parameters:
      length - 字符串的长度
      Returns:
      随机字符串
    • randomNumber

      public static int randomNumber()
      随机数字,数字为0~9单个数字
      Returns:
      随机数字字符
    • randomChar

      public static char randomChar()
      随机字母或数字,小写
      Returns:
      随机字符
    • randomChar

      public static char randomChar(String baseString)
      随机字符
      Parameters:
      baseString - 随机字符选取的样本
      Returns:
      随机字符
    • randomDay

      public static DateTime randomDay(int min, int max)
      以当天为基准,随机产生一个日期
      Parameters:
      min - 偏移最小天,可以为负数表示过去的时间(包含)
      max - 偏移最大天,可以为负数表示过去的时间(不包含)
      Returns:
      随机日期(随机天 , 其它时间不变)
    • randomDate

      public static DateTime randomDate(Date baseDate, Fields.Type type, int min, int max)
      以给定日期为基准,随机产生一个日期
      Parameters:
      baseDate - 基准日期
      type - 偏移的时间字段,例如时、分、秒等
      min - 偏移最小量,可以为负数表示过去的时间(包含)
      max - 偏移最大量,可以为负数表示过去的时间(不包含)
      Returns:
      随机日期
    • randomBoolean

      public static boolean randomBoolean()
      获得随机Boolean值
      Returns:
      true or false
    • randomChinese

      public static char randomChinese()
      随机汉字('一'-'鿿')
      Returns:
      随机的汉字字符