java8位随机数_JAVA⽣成指定位数的随机数public class Rondom {
public static String genRandomNum(int card_len){
//35是因为数组是从0开始的,26个字母+10个数字
final int maxNum = 36;
int i; //⽣成的随机数
int count = 0; //⽣成的密码的长度
char[] str = { ‘0‘, ‘1‘, ‘2‘, ‘3‘, ‘4‘, ‘5‘, ‘6‘, ‘7‘, ‘8‘, ‘9‘ };
StringBuffer pwd = new StringBuffer("");
Random r = new Random();
while(count
//⽣成随机数,取绝对值,防⽌⽣成负数
i = Math.Int(maxNum)); //⽣成的数最⼤为36-1
if (i >= 0 && i
pwd.append(str[i]);
count ++;
}
}
String();
java生成随机数的方法}
public static void main(String[] args) {
for(int l=0;l<100;l++){
System.out.println(genRandomNum(6));//⽣成6位的随机数
}
}
}