diff --git a/IDEA/src/main/java/com/example/api/utils/RandomUtil.java b/IDEA/src/main/java/com/example/api/utils/RandomUtil.java new file mode 100644 index 00000000..6f75e23c --- /dev/null +++ b/IDEA/src/main/java/com/example/api/utils/RandomUtil.java @@ -0,0 +1,27 @@ +package com.example.api.utils; + +import java.util.Random; + +/** + * RandomUtil 类提供生成随机数字字符串的功能。 + */ +public final class RandomUtil { + + /** + * 生成一个长度为6位的随机数字字符串。 + * @return 一个由6个随机数字组成的字符串。 + */ + public static String next() { + // 创建一个 StringBuilder 对象用于构建结果字符串 + StringBuilder builder = new StringBuilder(); + // 创建一个 Random 对象用于生成随机数 + Random random = new Random(); + // 循环6次,每次生成一个随机数字并追加到 builder 中 + for (int i = 0; i < 6; i++) { + // random.nextInt(10) 生成一个 0 到 9 之间的随机整数 + builder.append(random.nextInt(10)); + } + // 将构建好的随机数字字符串返回 + return builder.toString(); + } +}