/**
* create by 星航指挥官
* create on 2020/11/23
* 不过是大梦一场空
* 不过是孤影照惊鸿
*/
public class VerificationCode {
/**
* 创建指定数量的随机字符串
* @param isNumber 是否是数字
* @param length
* @return
*/
public static String getVerificationCode(boolean isNumber, int length){
String resultStr = "";
String codeContent = isNumber ? "1234567890" : "1234567890abcdefghijkmnpqrstuvwxyz";
boolean flag = true;
do {
int count = 0;
for (int i = 0; i < length; i++) {
double randomCode = Math.random() * codeContent.length();
int code = (int) Math.floor(randomCode);
char c = codeContent.charAt(code);
if (('0' <= c) && (c <= '9')) {
count++;
}
resultStr += codeContent.charAt(code);
}
if (count >= 2) {
flag = false;
}
} while (flag);
return resultStr;
}
}