import java.util.*;public class Test { public static void main(String[] args){ ArrayList arr = new Test().getRandom(50); Iterator it = arr.iterator(); while(it.hasNext()){ System.out.println(it.next()); } } public ArrayList getRandom(int num){ ArrayList randomNum = new ArrayList(); Random random = new Random(); int temp = 0; for (int i = 0;i < num; i ++){ temp = random.nextInt(num); int nums = temp + 1; if (randomNum.indexOf(new Integer(nums)) == -1){ randomNum.add(new Integer(nums)); }else{ i -- ; } } return randomNum; } }
int x
x=(int)(Math.random())*100;
if(x>50) return x-50;
else return x;
while(src.size()<50){
Integer temp=new Random().nextInt(50);
src.add(temp);
}
for(Integer i:src){
System.out.println(i);
}
public static void main(String[] args){
ArrayList arr = new Test().getRandom(50);
Iterator it = arr.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
public ArrayList getRandom(int num){
ArrayList randomNum = new ArrayList();
Random random = new Random();
int temp = 0;
for (int i = 0;i < num; i ++){
temp = random.nextInt(num);
int nums = temp + 1;
if (randomNum.indexOf(new Integer(nums)) == -1){
randomNum.add(new Integer(nums));
}else{
i -- ;
}
}
return randomNum;
}
}
nums[0]=(int)(Math.random()*50);
for(int i=1;i<nums.length;i++){
boolean f=false;
do{
f=false;
nums[i]=(int)(Math.random()*50);
for(int j=0;j<i;j++){
if(nums[i]==nums[j]){
f=true;
break;
}
}
}while(f);
}
然后判断以前的数据是否和这个有重复.