publicclassRandomTest{ publicstaticvoidmain(String[] args){ Set<Integer> set = new HashSet<>(); Random rand = new Random();
int index = 0; while (true) { int s = rand.nextInt(33)+1; if (set.contains(s)) { s = rand.nextInt(33)+1; }else{ set.add(s); index++; } if (index == 6) { break; } }
publicclassRandomTest{ publicstaticvoidmain(String[] args){ ArrayList<Integer> red = new ArrayList<>(); Random rand = new Random(); for (int i = 0; i < 6; i++) { int s = rand.nextInt(33) + 1; if (CunZai(red, s)) { red.add(s); }else{ //这里注意,必须得i--,要不然,集合中如果有重复的元素就会空位置,必须让它回退到前一个重新生成 i--; } } }