Hutool Hutool
(opens new window)
🏡首页
📖指南
🎢最佳实践
💖支持
💡javaDoc (opens new window)
⏳更新记录 (opens new window)
  • 🍎gitee (opens new window)
  • 🍏github (opens new window)
(opens new window)
🏡首页
📖指南
🎢最佳实践
💖支持
💡javaDoc (opens new window)
⏳更新记录 (opens new window)
  • 🍎gitee (opens new window)
  • 🍏github (opens new window)
  • 快速入门

  • 核心(hutool-core)

    • AI(Hutool-ai)

    • 注解(annotation)

    • 数组(array)

    • JavaBean(bean)

    • 缓存(cache)

    • 类加载器(classloader)

    • 编码解码(codec)

    • 集合(collection)

    • 比较器(comparator)

    • 压缩(compress)

    • 类型转换(convert)

    • 数据(data)

    • 日期时间(date)

    • 异常(exception)

    • 函数(func)

    • IO流(io)

    • 语言特性(lang)

    • Map(map)

    • 数字数学(math)

    • 网络(net)

    • 对象池(pool)

    • 反射(reflect)

    • 正则(regex)

    • 服务提供(spi)

    • 聚合操作(stream)

    • 字符串文本(text)

    • 并发和线程(thread)

    • 树结构(tree)

    • 工具集合(util)

      • 简介
      • 随机数工具-RandomUtil
        • 1. 随机数字生成
          • 1.1 整数随机数
          • 1.2 长整数随机数
          • 1.3 浮点数随机数
        • 2. 随机字符串生成
          • 2.1 字母字符串
          • 2.2 数字字符串
          • 2.3 混合字符串
          • 2.4 随机UUID
        • 3. 随机布尔值和字符
          • 3.1 随机布尔值
          • 3.2 随机字符
        • 4. 随机数组和集合
          • 4.1 随机数组元素
          • 4.2 随机集合元素
          • 4.3 随机打乱数组
        • 5. 其他随机数功能
          • 5.1 随机日期
          • 5.2 随机枚举值
        • 6. 适用场景
      • ByteUtil
      • CharsetUtil
      • EnumUtil
      • JdkUtil
      • JNDIUtil
      • ObjUtil
      • RuntimeUtil
      • SystemUtil
    • XML操作(xml)

  • 定时任务(hutool-cron)

  • 加密(hutool-crypto)

  • 数据库(hutool-db)

  • 扩展(hutool-extra)

  • HTTP(hutool-http)

  • 日志(hutool-log)

  • 配置文件(hutool-setting)

  • JSON(hutool-json)

  • Office办公(hutool-poi)

  • 套接字(hutool-socket)

  • GUI(hutool-swing)

  • 指南
  • 核心(hutool-core)
  • 工具集合(util)
Hutool
2026-01-03
目录

随机数工具-RandomUtil

RandomUtil是Hutool中用于生成各种类型随机数的工具类,提供了丰富的随机数生成方法,包括随机字符串、随机数字、随机布尔值等,让随机数生成变得更加便捷和强大。

# 1. 随机数字生成

# 1.1 整数随机数

// 生成指定范围内的随机整数
int randomInt = RandomUtil.randomInt(1, 100);

// 生成0到指定值之间的随机整数
int randomInt = RandomUtil.randomInt(100);

// 生成随机整数
int randomInt = RandomUtil.randomInt();

# 1.2 长整数随机数

// 生成指定范围内的随机长整数
long randomLong = RandomUtil.randomLong(1L, 100L);

// 生成0到指定值之间的随机长整数
long randomLong = RandomUtil.randomLong(100L);

// 生成随机长整数
long randomLong = RandomUtil.randomLong();

# 1.3 浮点数随机数

// 生成指定范围内的随机浮点数
float randomFloat = RandomUtil.randomFloat(1.0f, 100.0f);

// 生成随机浮点数
float randomFloat = RandomUtil.randomFloat();

// 生成指定范围内的随机双精度浮点数
double randomDouble = RandomUtil.randomDouble(1.0, 100.0);

// 生成随机双精度浮点数
double randomDouble = RandomUtil.randomDouble();

# 2. 随机字符串生成

# 2.1 字母字符串

// 生成指定长度的随机字母字符串(大小写混合)
String randomStr = RandomUtil.randomString(10);

// 生成指定长度的随机大写字母字符串
String randomUpper = RandomUtil.randomUpperCase(10);

// 生成指定长度的随机小写字母字符串
String randomLower = RandomUtil.randomLowerCase(10);

# 2.2 数字字符串

// 生成指定长度的随机数字字符串
String randomNumbers = RandomUtil.randomNumbers(10);

# 2.3 混合字符串

// 生成指定长度的随机字母数字混合字符串
String randomChar = RandomUtil.randomChar(10);

// 生成指定长度的随机字符串,可指定字符集
String randomCustom = RandomUtil.randomString(10, "abc123");

# 2.4 随机UUID

// 生成随机UUID
String uuid = RandomUtil.randomUUID();

// 生成简化版UUID(去掉横杠)
String simpleUuid = RandomUtil.simpleUUID();

# 3. 随机布尔值和字符

# 3.1 随机布尔值

// 生成随机布尔值
boolean randomBool = RandomUtil.randomBoolean();

// 生成指定概率的随机布尔值(80%概率返回true)
boolean randomBool = RandomUtil.randomBoolean(0.8);

# 3.2 随机字符

// 生成随机字符
char randomChar = RandomUtil.randomChar();

// 从指定字符串中生成随机字符
char randomCustomChar = RandomUtil.randomChar("abc123");

# 4. 随机数组和集合

# 4.1 随机数组元素

// 从数组中随机选取一个元素
String[] array = {"a", "b", "c", "d"};
String randomElement = RandomUtil.randomEle(array);

// 从数组中随机选取多个不重复元素
String[] randomElements = RandomUtil.randomEle(array, 2);

# 4.2 随机集合元素

// 从集合中随机选取一个元素
List<String> list = Arrays.asList("a", "b", "c", "d");
String randomElement = RandomUtil.randomEle(list);

// 从集合中随机选取多个不重复元素
List<String> randomElements = RandomUtil.randomEle(list, 2);

# 4.3 随机打乱数组

// 随机打乱数组
int[] array = {1, 2, 3, 4, 5};
RandomUtil.shuffle(array);

# 5. 其他随机数功能

# 5.1 随机日期

// 生成指定范围内的随机日期
Date start = DateUtil.parse("2020-01-01");
Date end = DateUtil.parse("2025-12-31");
Date randomDate = RandomUtil.randomDate(start, end);

# 5.2 随机枚举值

// 生成随机枚举值
enum TestEnum {
    A, B, C, D
}
TestEnum randomEnum = RandomUtil.randomEnum(TestEnum.class);

# 6. 适用场景

RandomUtil适用于以下场景:

  • 需要生成随机验证码时
  • 需要生成随机密码时
  • 需要生成随机测试数据时
  • 需要生成随机UUID时
  • 需要进行随机抽样时
  • 需要生成随机日期时
  • 需要生成随机枚举值时

通过RandomUtil,开发者可以更加便捷地生成各种类型的随机数,编写更简洁、更易维护的代码,同时享受到更丰富的随机数生成功能。

简介
ByteUtil

← 简介 ByteUtil→

Theme by Vdoing | Copyright © 2025-2026 Hutool | Apache-2.0
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式