优化cn.lili.common.utils.CommonUtil.getRandomNum方法逻辑

This commit is contained in:
*** 2022-03-27 07:41:34 +08:00
parent 36ce7593a9
commit de5e97ac3a

View File

@ -1,7 +1,7 @@
package cn.lili.common.utils; package cn.lili.common.utils;
import java.util.Random;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
/** /**
* 通用工具 * 通用工具
@ -9,6 +9,8 @@ import java.util.UUID;
*/ */
public class CommonUtil { public class CommonUtil {
public static final String BASE_NUMBER = "0123456789";
/** /**
* 以UUID重命名 * 以UUID重命名
* @param fileName 文件名称 * @param fileName 文件名称
@ -24,12 +26,12 @@ public class CommonUtil {
* 随机6位数生成 * 随机6位数生成
*/ */
public static String getRandomNum() { public static String getRandomNum() {
StringBuilder sb = new StringBuilder(6);
Random random = new Random(); for (int i = 0; i < 6; i++) {
int num = random.nextInt(999999); int num = ThreadLocalRandom.current().nextInt(BASE_NUMBER.length());
//不足六位前面补0 sb.append(BASE_NUMBER.charAt(num));
String str = String.format("%06d", num); }
return str; return sb.toString();
} }
} }