Class RandomKit

java.lang.Object
org.miaixz.bus.core.xyz.RandomKit

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

    • BASE_CHAR_NUMBER

      public static final String BASE_CHAR_NUMBER
      用于随机选的字符和数字(包括大写和小写字母)
  • Constructor Details

    • RandomKit

      public RandomKit()
  • Method Details

    • getRandom

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

      注意:此方法返回的ThreadLocalRandom不可以在多线程环境下共享对象,否则有重复随机数问题。 见:https://www.jianshu.com/p/89dfe990295c

      Returns:
      ThreadLocalRandom
    • createSecureRandom

      public static SecureRandom createSecureRandom(byte[] seed)
      创建SecureRandom,类提供加密的强随机数生成器 (RNG)
      Parameters:
      seed - 自定义随机种子
      Returns:
      SecureRandom
    • getSecureRandom

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

      相关说明见:how-to-solve-slow-java-securerandom

      Returns:
      SecureRandom
    • getSecureRandom

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

      相关说明见:how-to-solve-slow-java-securerandom

      Parameters:
      seed - 随机数种子
      Returns:
      SecureRandom
      See Also:
    • getSHA1PRNGRandom

      public static SecureRandom getSHA1PRNGRandom(byte[] seed)
      获取SHA1PRNG的SecureRandom,类提供加密的强随机数生成器 (RNG) 注意:此方法获取的是伪随机序列发生器PRNG(pseudo-random number generator),在Linux下噪声生成时可能造成较长时间停顿。 see: http://ifeve.com/jvm-random-and-entropy-source/

      相关说明见:how-to-solve-slow-java-securerandom

      Parameters:
      seed - 随机数种子
      Returns:
      SecureRandom
    • getSecureRandomStrong

      public static SecureRandom getSecureRandomStrong()
      获取algorithms/providers中提供的强安全随机生成器 注意:此方法可能造成阻塞或性能问题
      Returns:
      SecureRandom
    • getRandom

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

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

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

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

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

      public static int randomInt()
      获得随机数int值
      Returns:
      随机数
      See Also:
    • randomInt

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

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

      public static int randomInt(int min, int max, boolean includeMin, boolean includeMax)
      获得指定范围内的随机数
      Parameters:
      min - 最小数
      max - 最大数
      includeMin - 是否包含最小值
      includeMax - 是否包含最大值
      Returns:
      随机数
    • randomInts

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

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

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

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

      public static long randomLong(long min, long max, boolean includeMin, boolean includeMax)
      获得指定范围内的随机数
      Parameters:
      min - 最小数
      max - 最大数
      includeMin - 是否包含最小值
      includeMax - 是否包含最大值
      Returns:
      随机数
    • randomFloat

      public static float randomFloat()
      获得随机数[0, 1)
      Returns:
      随机数
      See Also:
    • randomFloat

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

      public static float randomFloat(float minInclude, float maxExclude)
      获得指定范围内的随机数[min, max)
      Parameters:
      minInclude - 最小数(包含)
      maxExclude - 最大数(不包含)
      Returns:
      随机数
      See Also:
    • randomDouble

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

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

      public static double randomDouble()
      获得随机数[0, 1)
      Returns:
      随机数
      See Also:
    • 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:
      随机数
      See Also:
    • 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 limitExclude)
      获得指定范围内的随机数 [0,limit)
      Parameters:
      limitExclude - 最大数(不包含)
      Returns:
      随机数
    • randomBigDecimal

      public static BigDecimal randomBigDecimal(BigDecimal minInclude, BigDecimal maxExclude)
      获得指定范围内的随机数
      Parameters:
      minInclude - 最小数(包含)
      maxExclude - 最大数(不包含)
      Returns:
      随机数
    • 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:
      随机元素
    • randomPick

      public static <T> List<T> randomPick(List<T> source, int count)
      随机获得列表中的一定量的元素,返回List 此方法与randomEles(List, int) 不同点在于,不会获取重复位置的元素
      Type Parameters:
      T - 元素类型
      Parameters:
      source - 列表
      count - 随机取出的个数
      Returns:
      随机列表
    • randomPickInts

      public static int[] randomPickInts(int size, int[] seed)
      生成从种子中获取随机数字
      Parameters:
      size - 指定产生随机数的个数
      seed - 种子,用于取随机数的int池
      Returns:
      随机int数组
    • 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:
      随机字符串
    • randomStringLower

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

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

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

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

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

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

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

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

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

      public static <T> WeightRandomSelector<T> weightRandom(WeightObject<T>[] weightObjs)
      带有权重的随机生成器
      Type Parameters:
      T - 随机对象类型
      Parameters:
      weightObjs - 带有权重的对象列表
      Returns:
      WeightRandomSelector
    • weightRandom

      public static <T> WeightRandomSelector<T> weightRandom(Iterable<WeightObject<T>> weightObjs)
      带有权重的随机生成器
      Type Parameters:
      T - 随机对象类型
      Parameters:
      weightObjs - 带有权重的对象列表
      Returns:
      WeightRandomSelector
    • 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:
      随机日期