Random random = new Random();
//随机产生一个布尔值
boolean b = random.nextBoolean();
System.out.println(b);
//随机产生一个整型数 随机范围是int的表述范围
int r = random.nextInt();
System.out.println(r);
//[0 10)
r = random.nextInt(10); //不包含10
//[10 100)
r = random.nextInt(91) + 10;
System.out.println(r);
//[0.0 1.0)
random.nextDouble();
eg1:产生一个六位的验证码,需要包含数字和字母
char[] content = new char[62];
//'0' —— '9'
for(int i = 0; i < 10; i++) {
content[i] = (char)('0' + i);
}
//'a' —— 'z'
for(int i = 10; i < 36; i++) {
content[i] = (char)('a' + i - 10);
}
//'A' —— 'Z'
for(int i = 36; i < content.length; i++) {
content[i] = (char)('A' + i - 36 );
}
//(种子数)
Random random = new Random();
String s = "";
for(int i = 0; i < 6; i++) {
int index = random.nextInt(62);
//System.out.println(index);
s += content[index];
}
System.out.println(s);
eg2:抽奖活动
String[] test = {"回家","待在西安","成都"};
System.out.println(test[random.nextInt(2)]);