JAVA模拟大乐透数据(仅供参考)

闲着无聊写了个大乐透数据规则,不说了贴代码上去吧。

private List<String> hiveMeAPrize(){
        List<String> strings = new ArrayList<>();
        for(int x = 0; x < 5000; x++){
            String s = "";
            int[] a = randomNums(1, 35, 5);
            int[] b = randomNums(1, 12, 2);
            Arrays.sort(a);
            Arrays.sort(b);
            for(int i : a){
                s += i +" ";
            }
            s += "----";
            for(int i : b){
                s += i +" ";
            }
            strings.add(s);
        }
        List<String> stringss = new ArrayList<>();
        for(String s : strings){
            int a = -1;
            for(String ss : strings){
                if(s.equals(ss)){
                    a++;
                }
            }
            stringss.add(s += "重复次数" + a);
        }
        return stringss;
    }

    public static int[] randomNums(int min, int max, int n) {
        int range = max - min + 1;
        if (min > max || n < 0 || n > range) {
            return null;
        }
        int[] nums = new int[n];
        List<Integer> list = new ArrayList<Integer>(n);
        while (list.size() < n) {
            int num = new Random().nextInt(range) + min;
            if (!list.contains(Integer.valueOf(num))) {
                nums[list.size()] = num;
                list.add(Integer.valueOf(num));
            }
        }
        return nums;
    }

判断是否重复的随便写的逻辑。o.o!!!

发布了39 篇原创文章 · 获赞 19 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/u010090644/article/details/83930790