生成随机数
做一串8位随机数包含大写字母和两个数字
public class Main3 {
public static void main(String[] args) {
Random random = new Random();
// int j=random.nextInt(2) % 2;
// System.out.println(j);
System.out.println(getRundom());
}
public static String getRundom(){
StringBuffer id=new StringBuffer();
id.append("XY");
Random random = new Random();
int index =0;
for (int i = 0; i < 6; i++) {
char s = 0;
int j=random.nextInt(2) % 3;
if(index ==2){
j = 1;
}
switch (j) {
case 0:
//随机生成数字
++index;
s = (char) (random.nextInt(10)+'0');
break;
case 1:
//随机生成大写字母
s = (char) (random.nextInt(26)+65);
break;
default:
}
id.append(s);
}
return id.toString();
}
}
public class Test22 {
public static void main(String[] args) {
System.out.println(getRundom());
}
public static String getRundom(){
StringBuffer id=new StringBuffer();
id.append("XY");
Random random = new Random();
int index =0;
for (int i = 0; i < 6; i++) {
char s = 0;
int j=random.nextInt(2) % 4;
if(index ==2){
j = 1;
}
switch (j) {
case 0:
//随机生成数字
++index;
s = (char) (random.nextInt(57) % (57 - 48 + 1) + 48);
break;
case 1:
//随机生成大写字母
s = (char) (random.nextInt(90) % (90 - 65 + 1) + 65);
break;
default:
}
id.append(s);
}
return id.toString();
}
}