1.定义随机的生成数字,作为数组下标
public Integer getRandomInt(int num) {
//将传来的num组装成0-num的list集合
List valueRange=new ArrayList();
if (num!=0){
valueRange= assembleList(num);
}else{//若未定义长度,取当前时间分分钟数+10作为种子
Calendar now = Calendar.getInstance();
int temp=now.get(Calendar.MINUTE)+10;//防止0分
valueRange=assembleList(temp);
}
Collections.shuffle(valueRange);//打乱集合
return valueRange.get(0);//取打乱后的第一个值作为随机数
}
//组装成num长度的list集合
public List assembleList(int num){
List reslist=new ArrayList();
for(int i=0;i
reslist.add(i);
}
return reslist;
}
2.定义随机的字符数组
int rondomNum=(getRandomInt(16))%9 +8;
int maxNum=80;
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',
'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',
'-','.','~','!','@','#','$','%','^','&','*','(',')','_',':','<','>','?'};
StringBuffer newPSW=new StringBuffer("");
while(count<rondomNum){
i= Math.abs(getRandomInt(maxNum));
if(i>=0&&i <str.length){
newPSW.append(str[i]);
count++;
}
}