UUID是Universally Unique Identifier的缩写,它是在一定的范围内(从特定的名字空间到全球)唯一的机器生成的标识符。
public static void main(String[] args) {
//获得六位验证码(包括小写字母和数字,不包括大写字母)
// String result = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 6);
UUID uuid = UUID.randomUUID();
String string1 = uuid.toString();
String string2 = string1.replaceAll("-", "");
String string3 = string2.substring(0, 6);
System.out.println("验证码为:" + uuid);//77be56a9-b516-4739-8f04-61a11a2e25d4
System.out.println("验证码为:" + string1);//77be56a9-b516-4739-8f04-61a11a2e25d4
System.out.println("验证码为:" + string2);//77be56a9b51647398f0461a11a2e25d4
System.out.println("验证码为:" + string3);//77be56
}
不使用UUID获取随机:
public static void main(String[] args) {
// 取随机产生的认证码(6位数字)
StringBuffer sb = new StringBuffer();
for (int i = 0; i < 6; i++) {
String str = getRandomChar();
sb.append(str);
}
System.out.print("验证码为:" + sb.toString());
}
public static String getRandomChar() {
int index = (int) Math.round(Math.random() * 2);//随机选择大小写字母还是数字[0,2)
String randChar = "";
switch (index) {
case 0:// 大写字符
randChar = String.valueOf((char) Math.round(Math.random() * 25 + 65));
break;
case 1:// 小写字符
randChar = String.valueOf((char) Math.round(Math.random() * 25 + 97));
break;
default:// 数字
randChar = String.valueOf(Math.round(Math.random() * 9));
break;
}
return randChar;
}
综上可得:
public static void main(String[] args) {
System.out.println(getCode(10, 1).toString());// 数字
System.out.println(getCode(10, 2).toString());// 小写字母
System.out.println(getCode(10, 3).toString());// 大写字母
System.out.println(getCode(10, 4).toString());// 数字+大、小写字母
System.out.println(getCode(10, 5).toString());// 数字+大、小写字母
System.out.println(getCode(10, 6).toString());//UUID去掉连字符的32位字符串
}
public static String getCode(int passLength, int type) {
StringBuffer buffer = null;
StringBuffer sb = new StringBuffer();
Random random = new Random();
random.setSeed(new Date().getTime());
switch (type) {
case 1:
buffer = new StringBuffer("0123456789");// 数字
break;
case 2:
buffer = new StringBuffer("abcdefghijklmnopqrstuvwxyz");// 小写字母
break;
case 3:
buffer = new StringBuffer("ABCDEFGHIJKLMNOPQRSTUVWXYZ");// 大写字母
break;
case 4:
buffer = new StringBuffer(
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");// 数字+小写、大写字母
break;
case 5:
buffer = new StringBuffer(
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");// 数字+小写、大写字母
sb.append(buffer.charAt(random.nextInt(buffer.length() - 10)));//26+26+10-10=52->[0-52)位
passLength -= 1;
break;
case 6:
String s = UUID.randomUUID().toString();
//随机36位UUID(包括4个连字符位置:8,13,18,23)
//如:0e6c14a9-eb23-480e-99ad-cf320fc4c843
//substring(a,b):取到a取不到b
//sb.append(s.replaceAll("-", ""));
sb.append(s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18) + s.substring(19, 23) + s.substring(24));
//去掉连字符,然后取到32位,如:eee671a5 bd0e 4774 b38a ebf862867fd4
}
//如果不是UUID
if (type != 6) {
int range = buffer.length();//type对应的buffer的长度[10]或者[26]或者[36]
for (int i = 0; i < passLength; i++) {//验证码的位数
sb.append(buffer.charAt(random.nextInt(range)));//依次将对应下标(随机)的字符添加到sb中
//random.nextInt(range)为[0,10)或者[0,26)中随机一个
//buffer.charAt(random.nextInt(range))为buffer取对应下标的字符
}
}
return sb.toString();//将字符序列转换为字符串
}