第一种
public static void main(String[] args) {
HashSet list=new HashSet();
HashSet list1=suiJi(list);
sop(list1);
}
public static HashSet suiJi(HashSet al) {
Random rd=new Random();
int m=0;
String n="";
String a="";
int b=0;
for (int i = 0; i < 4; i++) {
String str="abcdefhijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXY";
String str1="345678";
m=rd.nextInt(46);
n=""+str.charAt(m);
b=rd.nextInt(6);
a=""+str1.charAt(b);
b=rd.nextInt(2);
if(b==0) {
al.add(a);
}else {
al.add(n);
}
}
return al;
}
第二种
public class ThreadWork {
//生成随机数字和字母,
public String getStringRandom(int length) {
String val = "";
Random random = new Random();
//参数length,表示生成几位随机数
for(int i = 0; i < length; i++) {
String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
//输出字母还是数字
if( "char".equalsIgnoreCase(charOrNum) ) {
//输出是大写字母还是小写字母
int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
val += (char)(random.nextInt(26) + temp);
} else if( "num".equalsIgnoreCase(charOrNum) ) {
val += String.valueOf(random.nextInt(10));
}
}
return val;
}
public static void main(String[] args) {
ThreadWork test = new ThreadWork();
//测试
System.out.println(test.getStringRandom(8));
}
}