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()); }}

解决方案 »

  1.   

    这个我是用洗牌算法作的.也是自己在回家的路上路过一个彩票站,看一堆堆的人在买.
    自己没有钱去玩真的就作一个模拟的试试.
    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());
    } }
    ================================
    让它循环了100次,看了一下结果.感觉自己还是没有那必要玩真的了.
    呵呵,开玩笑的.
      

  2.   


     SimulateLottery sl = new SimulateLottery(22, 5);
                System.out.println(sl.getResult());===============================
    这不就是你的要的效果吗.
    几选几是可以变的参数.
    只要改变构造函数中所传入的值就可以了.
      

  3.   

    时间关系就暂时只写了一个构造函数可以运行.
     SimulateLottery sl = new SimulateLottery(36, 9);
    有时间了再整理的有条理性些.
      

  4.   


    ==========================
    我是快下班的时候没有事,就想起来了这个.就利用几分钟时间简单写了个.
    我想有时间了再整理一下.好好重构一下.然后作成窗口形式的并可以在没有jre的机子上运行.这也是有时间了才会重新完善一下.谢谢你的建议.