Class RandomUtil

java.lang.Object
com.sure.tool.util.RandomUtil

public class RandomUtil extends Object
随机工具类,参考 Hutool 的 RandomUtil 设计。

English: Random string, number and element selection helpers.

Since:
0.1.0
Author:
suretool
  • Field Details

  • Method Details

    • randomInt

      public static int randomInt()
      随机 int。
      Returns:
      随机 int
    • randomInt

      public static int randomInt(int min, int max)
      随机 int,范围 [min, max)。
      Parameters:
      min - 最小值(含)
      max - 最大值(不含)
      Returns:
      随机 int
    • randomInt

      public static int randomInt(int limit)
      随机 int,范围 [0, limit)。
      Parameters:
      limit - 上限(不含)
      Returns:
      随机 int
    • randomLong

      public static long randomLong()
      随机 long。
      Returns:
      随机 long
    • randomLong

      public static long randomLong(long min, long max)
      随机 long,范围 [min, max)。
      Parameters:
      min - 最小值(含)
      max - 最大值(不含)
      Returns:
      随机 long
    • randomDouble

      public static double randomDouble()
      随机 double,范围 [0, 1)。
      Returns:
      随机 double
    • randomDouble

      public static double randomDouble(double min, double max)
      随机 double,范围 [min, max)。
      Parameters:
      min - 最小值(含)
      max - 最大值(不含)
      Returns:
      随机 double
    • randomDouble

      public static double randomDouble(double min, double max, int scale)
      随机 double,范围 [min, max),保留指定小数位。
      Parameters:
      min - 最小值(含)
      max - 最大值(不含)
      scale - 小数位
      Returns:
      随机 double
    • randomBoolean

      public static boolean randomBoolean()
      随机布尔值。
      Returns:
      随机布尔值
    • randomString

      public static String randomString(int length)
      随机字符串(字母 + 数字)。
      Parameters:
      length - 长度
      Returns:
      随机字符串
    • randomString

      public static String randomString(String baseStr, int length)
      从指定字符集中随机字符串。
      Parameters:
      baseStr - 字符集
      length - 长度
      Returns:
      随机字符串
    • randomNumbers

      public static String randomNumbers(int length)
      随机数字字符串。
      Parameters:
      length - 长度
      Returns:
      随机数字字符串
    • randomBytes

      public static byte[] randomBytes(int count)
      随机字节数组。
      Parameters:
      count - 字节数
      Returns:
      随机字节数组
    • randomUUID

      public static String randomUUID()
      随机 UUID(含连字符)。
      Returns:
      UUID 字符串
    • simpleUUID

      public static String simpleUUID()
      随机 UUID(不含连字符)。
      Returns:
      UUID 字符串
    • randomEle

      public static <T> T randomEle(List<T> list)
      从集合中随机取一个元素。
      Type Parameters:
      T - 类型
      Parameters:
      list - 列表
      Returns:
      随机元素;空列表返回 null
    • randomEle

      public static <T> T randomEle(T[] array)
      从数组中随机取一个元素。
      Type Parameters:
      T - 类型
      Parameters:
      array - 数组
      Returns:
      随机元素;空数组返回 null
    • randomEleSet

      public static <T> Set<T> randomEleSet(Collection<T> coll, int count)
      随机取 count 个不重复元素。
      Type Parameters:
      T - 类型
      Parameters:
      coll - 集合
      count - 数量(不大于集合大小)
      Returns:
      随机子集
    • randomEleWeighted

      public static <T> T randomEleWeighted(Collection<WeightRandom.WeightObj<T>> weights)
      按权重随机取一个元素。
      Type Parameters:
      T - 元素类型
      Parameters:
      weights - 权重对象集合(WeightRandom.WeightObj)
      Returns:
      随机元素;集合为空返回 null
    • randomDay

      public static LocalDate randomDay(LocalDate start, LocalDate end)
      生成 [start, end] 区间内的随机日期。
      Parameters:
      start - 起始日期(含)
      end - 结束日期(含)
      Returns:
      随机日期
    • randomDay

      public static Date randomDay(Date start, Date end)
      生成 [start, end] 区间内的随机日期。
      Parameters:
      start - 起始日期(含)
      end - 结束日期(含)
      Returns:
      随机日期
    • randomBigDecimal

      public static BigDecimal randomBigDecimal(BigDecimal start, BigDecimal end)
      生成 [start, end] 区间内的随机 BigDecimal(含端点)。
      Parameters:
      start - 起始(含)
      end - 结束(含)
      Returns:
      随机小数(与区间端点同精度)
    • randomLetter

      public static String randomLetter(int count)
      生成随机小写字母字符串。
      Parameters:
      count - 长度
      Returns:
      随机字母串
    • randomChinese

      public static String randomChinese(int count)
      生成随机中文字符串(CJK 统一表意文字基本区)。
      Parameters:
      count - 长度
      Returns:
      随机汉字串
    • randomString

      public static String randomString(int count, char[] chars)
      从指定字符集生成随机字符串。
      Parameters:
      count - 长度
      chars - 字符集(可重复),为 null 或空时使用默认字母数字集
      Returns:
      随机字符串