需求: 6位邀请码,字母数字混合,数字最多不超过四位。
public static String getCode() {
String code = generateCode();
User user = userDao.selectByCode(code);
if(user != null){
return getCode();
}else{
return code ;
}
}
public static String generateCode() {
String charList = "ABCDEFGHIJKLMNPQRSTUVWXY";
String numList = "0123456789";
String rev = "";
int maxNumCount = 4;
int length = 6;
Random f = new Random();
for (int i = 0; i < length; i++) {
if (f.nextBoolean() && maxNumCount > 0) {
maxNumCount--;
rev += numList.charAt(f.nextInt(numList.length()));
} else {
rev += charList.charAt(f.nextInt(charList.length()));
}
}
return rev;
}