feat:生成随机数
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
package com.xiang.xservice.basic.utils;
|
||||
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
public class RandomCodeUtils {
|
||||
|
||||
private final static String CHAR = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ";
|
||||
|
||||
|
||||
/**
|
||||
* 生成随机位数数字码
|
||||
* @param length 数字位数
|
||||
* @return 数字随机数
|
||||
*/
|
||||
public static String getNumberRandomCode(int length) {
|
||||
StringBuilder sb = new StringBuilder(length);
|
||||
ThreadLocalRandom random = ThreadLocalRandom.current();
|
||||
for (int i = 0; i < length; i++) {
|
||||
sb.append(random.nextInt(0, 10));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成随即位数字母数字验证码
|
||||
* @param length 位数
|
||||
* @return 随机验证码
|
||||
*/
|
||||
public static String getMixtureRandomCode(int length) {
|
||||
StringBuilder sb = new StringBuilder(length);
|
||||
ThreadLocalRandom random = ThreadLocalRandom.current();
|
||||
for (int i = 0; i < length; i++) {
|
||||
sb.append(CHAR.charAt(random.nextInt(CHAR.length())));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user