62,620
社区成员
发帖
与我相关
我的任务
分享public class SimulateLottery {
private List result = new ArrayList();
public SimulateLottery() {
}
public SimulateLottery(int totalNum, int gender) {
List list = setSize(totalNum);
result = setGenderNum(list, gender);
}
public List getResult() {
return result;
}
public List setSize(int n) {
List list = new ArrayList();
for (int i = 0; i < n; i++) {
list.add(i+1);
}
shuf(list);
return list;
}
public void shuf(List list) {
Collections cons = null;
cons.shuffle(list);
}
public List setGenderNum(List list, int n) {
List resultList = new ArrayList();
Integer result;
for (int i = 0; i < n; i++) {
int o = getNextNum(list);
result = (Integer) list.get(o);
list.remove(o);
resultList.add(result);
}
return resultList;
}
public int getNextNum(List list) {
int temp = 0;
Random ran = new Random();
int n = list.size() - 1;
ran.setSeed(System.currentTimeMillis());
temp = ran.nextInt() % n;
while (temp <= 0) {
temp = ran.nextInt() % n;
}
return temp;
}
public static void main(String[] args) {
SimulateLottery sl = new SimulateLottery(36, 9);
System.out.println(sl.getResult());
}
} SimulateLottery sl = new SimulateLottery(22, 5);
System.out.println(sl.getResult());public static void main(String[] args) {
for(int i=0;i<100;i++){
//SimulateLottery sl = new SimulateLottery(所选范围大小也即从1到这个数的大小数,要选择出的个数);
SimulateLottery sl = new SimulateLottery(36, 9);
System.out.println(sl.getResult());
}
}