package org.hsweb.web.core.utils;
import java.util.Random;
import java.util.UUID;
/**
* 随机数工具,用于产生随机数,随机密码等
*/
public class RandomUtil {
private static final Random random = new Random();
public static Random getRandom() {
return random;
}
static char[] chars = {
'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u',
'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G',
'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U',
'V', 'W', 'X', 'Y', 'Z'
};
/**
* 随机生成由0-9a-zA-Z组合而成的字符串
*
* @param len 字符串长度
* @return 生成结果
*/
public static String randomChar(int len) {
StringBuffer shortBuffer = new StringBuffer();
String uuid = UUID.randomUUID().toString().replace("-", "");
for (int i = 0; i < len; i++) {
String str = uuid.substring(i * 4, i * 4 + 4);
int x = Integer.parseInt(str, 16);
shortBuffer.append(chars[x % 0x3E]);
}
return shortBuffer.toString();
}
public static String randomChar() {
return randomChar(8);
}
}