撸一个11选5随机任意不重复

闲来无事,简单写一个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;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值