public String getInvitationCode(int length) {
StringBuffer sbf = new StringBuffer();
Random random = new Random();
int length = 6;//准备生成几位随机数
//参数length,表示生成几位随机数
for(int i = 0; i < length; i++) {
String flag = random.nextInt(2) % 2 == 0 ? "char" : "num";
//输出字母还是数字
if( "char".equalsIgnoreCase(flag) ) {
//输出是大写字母还是小写字母
int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
sbf.append((char)(random.nextInt(26) + temp));
} else if( "num".equalsIgnoreCase(flag) ) {
sbf.append(String.valueOf(random.nextInt(10)));
}
}
return sbf.toString();
}
大写字母ascii码:65~90
小写字母ascii码:97~122
转载于:https://blog.51cto.com/lailai/1711279