java生成不重复的随机数
import java.util.*;
public class Test{
public static void main(String[] args){
//生成 [0-n) 个不重复的随机数
//list 用来保存这些随机数
ArrayList list = new ArrayList();
int n = 10;
Random rand = new Random();
boolean[] bool = new boolean[n];
int num =0;
for (int i = 0; i<n; i++){
do{
//如果产生的数相同继续循环
num = rand.nextInt(n);
}while(bool[num]);
bool[num] =true;
list.add(num);
}
System.out.println (list);
}
}
public class Test{
public static void main(String[] args){
//生成 [0-n) 个不重复的随机数
//list 用来保存这些随机数
ArrayList list = new ArrayList();
int n = 10;
Random rand = new Random();
boolean[] bool = new boolean[n];
int num =0;
for (int i = 0; i<n; i++){
do{
//如果产生的数相同继续循环
num = rand.nextInt(n);
}while(bool[num]);
bool[num] =true;
list.add(num);
}
System.out.println (list);
}
}