展开全部
生成随机可以用Random类,不能重复可以考虑放在HashSet中,因为HashSet的值是不能重复的,看代码如下62616964757a686964616fe4b893e5b19e31333363363531:public class GenerateRandomLetterAndNum {
public static void main(String[] args) {
Set store = getletterandnum(6);
printSet(store);
}
public static Set getletterandnum(int length) {
Set set =new HashSet();
for (int i = 0; i
String value = getrandom();
set.add(value);
}
if (set.size()
String value = getrandom();//继续调用生成随机数的方法
set.add(value);
}
return set;
}
private static String getrandom() { //生成随机字母和数字方法
String value = "";
Random random = new Random();
int gen = random.nextInt(2);//0、1、2
String charornum = gen % 2 == 0 ? "char" : "num";
if ("char".equals(charornum)) {
int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
int ascii = random.nextInt(26);
value += (char) (ascii + temp);
} else if ("num".equalsIgnoreCase(charornum)) {
value += String.valueOf(random.nextInt(10));
}
return value;
}
public static void printSet(Set set){ //打印set的方法
Iterator iterator = set.iterator();
while (iterator.hasNext()) {
String ele = (String) iterator.next();
System.out.print(ele+" ");
}
}