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()); }}
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()); }}
解决方案 »
- java通过jacob读取ppt对象的属性的问题
- java 发送HTTP POST 出现500错误
- swing中的JTabbedPane标签
- 请问::下面代码那有错误?怎么改?谢谢!
- acess 数据库连接错误
- socket程序线程的问题:
- 调查一下,大家看thinking in java用了多久,看完之后的java水平怎样?
- 在JBuilder下如何生成 .class文件?
- Comm包在JBuider下注册过,但程序中还是找不到COM1口。。
- 为什么我上传文件的时候,如果遇到文件名是中文的,虽然可以上传,但不可以下载,怎么解决?
- 新手问很一下这个程序.....
- 关于PriorityQueue
自己没有钱去玩真的就作一个模拟的试试.
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次,看了一下结果.感觉自己还是没有那必要玩真的了.
呵呵,开玩笑的.
SimulateLottery sl = new SimulateLottery(22, 5);
System.out.println(sl.getResult());===============================
这不就是你的要的效果吗.
几选几是可以变的参数.
只要改变构造函数中所传入的值就可以了.
SimulateLottery sl = new SimulateLottery(36, 9);
有时间了再整理的有条理性些.
==========================
我是快下班的时候没有事,就想起来了这个.就利用几分钟时间简单写了个.
我想有时间了再整理一下.好好重构一下.然后作成窗口形式的并可以在没有jre的机子上运行.这也是有时间了才会重新完善一下.谢谢你的建议.