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

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

 

private List hiveMeAPrize(){
        List 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 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 list = new ArrayList(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!!!

你可能感兴趣的:(java)