闲来无事,简单写一个11选5的随机不重复,本地跑一下,没事买几注,中奖率当然是平等的,萌萌哒的气质不能丢的呀,?
public static void main(String[] args) {
// examp:任三 5注
Set set = generateNumber(3, 5);
System.out.println(set);
// examp:任三 的最大组合
getMaxFindCount(3);
}
/**
* 输出当前组合数内最大出现次数
* @param size 想要任意的数 比如任3 直接入3
*/
public static void getMaxFindCount(int size){
for (int i = 0; i < Integer.MAX_VALUE; i++) {
try {
generateNumber(size, i);
}catch (StackOverflowError e) {
System.out.println(i);
break;
}
}
}
/**
*
* @param size 想要任意的数 比如任3 直接入3
* @param count 想要出现几组 比如想随机10 输入10
* @return
*/
public static Set generateNumber(int size, int count) {
Set set = new HashSet<>();
return loop(set, size, count);
}
public static Set loop(Set set, int size, int count){
List<Integer> result = new ArrayList<>(size);
Random random = new Random();
int[] container = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
int index;
int value;
for (int i = 1; i <= size; i++) {
index = random.nextInt(container.length);
value = container[index];
result.add(value);
container = removeIndex(container, index);
}
Collections.sort(result);
set.add(result);
if (set.size() < count) {
loop(set, size, count);
}
return set;
}
public static int[] removeIndex(int[] container, int index) {
int i = 0;
int otherIndex = 0;
int[] arr1 = new int[container.length - 1];
while (i < container.length) {
if (i != index) {
arr1[otherIndex] = container[i];
otherIndex++;
}
i++;
}
return arr1;
}