可以使用JDK中提供的用于生成随机数的类java.util.Random,其中: *构造方法Random()
,用于创建一个新的随机数生成器对象。 *public int nextInt(int
n)方法返回一个伪随机数,它是取自此随机数生成器序列的、在 0(包括)和指定值(不包括)之间均匀分布的 int 值
private static List<Integer> list = new ArrayList<Integer>();
public static void main(String[] args) {
for(int i = 1; i < 1001; i ++){
list.add(new Integer(i));
}
for(int i = 0; i < 900; i ++){
int random = new Random().nextInt(list.size());
System.out.println("第"+(i + 1)+"个:" + list.get(random));
list.remove(random);
public void random(int n) {
// init
int[] array = new int[n];
for (int i = 0; i < n; i++)
array[i] = i + 1;
// core
Random r = new Random();
int count = n, index;
while (count > 100) {
index = r.nextInt(count);
System.out.println(array[index]);
array[index] = array[--count];
}
}
public List geiList(){
ArrayList list = new ArrayList();
int x = 0;
Random ran = new Random();
while(true){
x = ran.nextint(1000) + 1;
if(list.isRmpty()){
list.add(x);
}
if(list.size()==900){
return list;
}
return list;
}
}