双色球随机算法

来源:互联网 发布:centos 6.9 发布日期 编辑:程序博客网 时间:2024/05/21 09:02

package javatest;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class ball {
Random r = new Random();

public Set getRed() {
Set redSet = new HashSet();
for (int i = 0; i < 100; i++) {
if (redSet.add((int) r.nextInt(32) + 1)) {
if (redSet.size() == 6) {
break;
}
} else {
continue;
}
}
return redSet;
}

public int getBlue() {
int blue = r.nextInt(11) + 1;
return blue;
}

public static void main(String[] args) {
for (int j = 0; j < 5; j++) {
ball b = new ball();
Set redSet = b.getRed();
int blue = b.getBlue();
System.out.println("兰球号码是:" + redSet + ",红球号码是:" + blue);
}
}
}

原创粉丝点击