public String genRandomNum(){
int maxNum = 36;
int i;
int count = 0;
char[] str = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
StringBuffer pwd = new StringBuffer("");
Random r = new Random();
while(count < 8){
i = Math.abs(r.nextInt(maxNum));
if (i >= 0 && i < str.length) {
pwd.append(str[i]);
count ++;
}
}
return pwd.toString();
}
本文介绍了一个使用Java编写的随机字符串生成器,该生成器能够创建包含大写字母和数字的8位随机字符串。通过使用StringBuffer和Random类,确保了生成的字符串在每次调用时都是唯一的。
267

被折叠的 条评论
为什么被折叠?



