生成随机数字和字母
//生成随机数字和字母,
public String getEnglishAndIntRandom(int length) {
String value = "";
Random random = new Random();
//length为几位密码
for(int i = 0; i < length; i++) {
String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
//输出字母还是数字
if( "char".equalsIgnoreCase(charOrNum) ) {
//输出是大写字母还是小写字母
int tmp = random.nextInt(2) % 2 == 0 ? 65 : 97;
value += (char)(random.nextInt(26) + tmp);
} else if( "num".equalsIgnoreCase(charOrNum) ) {
value += String.valueOf(random.nextInt(10));
}
}
return value;
}
//生成字母随机数
private static String getEnglishRandom(int length){
String eng = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
Random random = new Random();
StringBuffer sb = new StringBuffer();
for ( int i = 0; i < length; i++ ){
int number = random.nextInt(eng.length());
sb.append(eng.charAt(number));
}
return sb.toString();
}