public class verificationCode {
/*
* 生成随机6位数验证码,包括数字和字母
* 0~9 ascii:48~57
* a~z 是 97~122
* A~Z 是 65~90
*
* 1:生成一个1~3的随机数,取整,来表示随机生成 Math.floor(1+Math.random()*3)
* 2:生成48~57的随机数 Math.floor(48+Math.random()*10)
* 3:生成97~122的随机数 Math.floor(97+Math.random()*26)
* 3:生成65~90的随机数 Math.floor(65+Math.random()*26)
*
* */
public static String creatCode(){
char[] code =new char[6];
for (int i=0;i<6;i++){
int flag =(int) Math.floor(1+Math.random()*3);
switch (flag){
case 1 :
code[i] =(char) Math.floor(48+Math.random()*10);
break;
case 2:
code[i] = (char) Math.floor(97+Math.random()*26);
break;
case 3:
code[i] = (char) Math.floor(65+Math.random()*26);
break;
}
}
String codeString =new String(code);
return codeString;
}
}
随机生成6位验证码算法(数字,大小写字母)
最新推荐文章于 2024-05-09 20:30:09 发布