【问题标题】:Creating an algorithm for selecting multiple lottery numbers (mathematics and statistics) [closed]创建用于选择多个彩票号码的算法(数学和统计)[关闭]
【发布时间】:2019-03-21 19:18:55
【问题描述】:

在西班牙的投注系统中有一个叫做multiple的概念,意思是如果你想玩的游戏有6个数字的投注,你可以创建一个7或8个数字的特殊投注,甚至9、10或11个数字。该特殊投注将引用 6 个数字的 X 次正常投注,这些数字将结合给定的数字。

The multiple bet of 7 numbers will traduce in 7 bets of 6 numbers. 
The multiple bet of 8 numbers will traduce in 28 bets of 6 numbers.
The multiple bet of 9 numbers will traduce in 84 bets of 6 numbers.
The multiple bet of 10 numbers will traduce in 210 bets of 6 numbers.
The multiple bet of 11 numbers will traduce in 462 bets of 6 numbers.

数字为 1、2、3、4、5、6、7 的 7 的倍数示例:

234567 134567 124567 123567 123467 123457 123456

数字为 1、2、3、4、5、6、7、8 的 8 的倍数示例:

123456 123457 123458 123467 123468 123478 123567 123568 123578 123678 124567 124568 124578 124678 125678 134567 134568 134578 134678 135678 145678 234567 234568 234578 234678 235678 245678 345678

我的第一个目标是在 Java 中实现生成 multiples 的算法。我的意思是,每个赌注的成本为 1 个硬币,因此,例如,给定 30 个数字和 800 个硬币,将 800 个硬币浪费在 X 个 X 个数字的多次投注中。多重投注必须将 30 个号码的出现次数或多或少相等。

倍数的总成本必须接近800欧元,可以少一点,但不能超过800欧元。 该算法将提供不同的建议,例如,可以提供接近 800 欧元的结果和 7 的倍数、接近 800 的结果和 8 的倍数等......用户将选择哪个更喜欢。 我不知道如何实现这一点。

在这个网站有一个网络倍数生成器,可以生成7和8的倍数,但它的代码是不公开的:http://www.miramiprimi.miraestudio.es/MetodoMultiplePrimitiva.php

【问题讨论】:

  • 在给定资源上使用的算法非常简单。您确定也想使用它,还是对生成的排列中添加更多随机化更感兴趣?
  • @dbl 是的,我绝对确定我需要那个算法。有人投票赞成关闭,我不明白为什么。拜托,你能用你的算法做一个答案,向人们展示这是一个有答案的好问题吗?非常感谢你
  • 你正在尝试重新发明这个轮子(哈哈):Lottery Wheeling。希望这会有所帮助。

标签: java algorithm math statistics


【解决方案1】:

@NullPointerException 我今天早些时候的意思是,应用于给定 URL 的算法几乎是一种蛮力算法,用于生成 N 个符号集中长度为 6 的所有组合。它的代码非常简单:

private static List<List<Integer>> bruteForce(List<Integer> numbers) {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> current;
    for (int position1 = 0; position1 < numbers.size(); position1++) {
        for (int position2 = position1 + 1; position2 < numbers.size(); position2++) {
            for (int position3 = position2 + 1; position3 < numbers.size(); position3++) {
                for (int position4 = position3 + 1; position4 < numbers.size(); position4++) {
                    for (int position5 = position4 + 1; position5 < numbers.size(); position5++) {
                        for (int position6 = position5 + 1; position6 < numbers.size(); position6++) {
                            current = new ArrayList<>();

                            current.add(numbers.get(position1));
                            current.add(numbers.get(position2));
                            current.add(numbers.get(position3));
                            current.add(numbers.get(position4));
                            current.add(numbers.get(position5));
                            current.add(numbers.get(position6));

                            result.add(current);
                        }
                    }
                }
            }
        }
    }
    return result;
}

然后是一些用例: // 请记住它适用于 array_length >= 6!

public static void main(String[] args) {
    List<Integer> numbers;

    System.out.println("N = 6, resulting in 1 element in the list.");
    numbers = new ArrayList<Integer>() {{ add(1); add(2); add(3); add(4); add(5); add(6);}};
    bruteForce(numbers).forEach(System.out::println);

    System.out.println("N = 7, resulting in 7 element in the list.");
    numbers = new ArrayList<Integer>() {{ add(1); add(2); add(3); add(4); add(5); add(6); add(7);}};
    bruteForce(numbers).forEach(System.out::println);

    System.out.println("N = 8, resulting in 28 element in the list.");
    numbers = new ArrayList<Integer>() {{ add(1); add(2); add(3); add(4); add(5); add(6); add(7); add(8);}};
    bruteForce(numbers).forEach(System.out::println);

    System.out.println("N = 9, resulting in 84 element in the list.");
    numbers = new ArrayList<Integer>() {{ add(1); add(2); add(3); add(4); add(5); add(6); add(7); add(8); add(9);}};
    bruteForce(numbers).forEach(System.out::println);
}

输出将是:

N = 6, resulting in 1 element in the list.
[1, 2, 3, 4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7, 8, 9]
[2, 3, 6, 7, 8, 9]
[2, 4, 5, 6, 7, 8]
[2, 4, 5, 6, 7, 9]
[2, 4, 5, 6, 8, 9]
[2, 4, 5, 7, 8, 9]
[2, 4, 6, 7, 8, 9]
[2, 5, 6, 7, 8, 9]
[3, 4, 5, 6, 7, 8]
[3, 4, 5, 6, 7, 9]
[3, 4, 5, 6, 8, 9]
[3, 4, 5, 7, 8, 9]
[3, 4, 6, 7, 8, 9]
[3, 5, 6, 7, 8, 9]
[4, 5, 6, 7, 8, 9]

请记住,输出中使用的数字来自List&lt;Integer&gt; numbers = ... 的定义,因此您可以尝试并进一步测试它。另外我建议在这种情况下使用有序集合而不是列表。

喜欢:

numbers = new ArrayList<Integer>() {{ add(11); add(22); add(33); add(44); add(45); add(46); add(47);}};
bruteForce(numbers).forEach(System.out::println);

打印:

[11, 22, 33, 44, 45, 46]
[11, 22, 33, 44, 45, 47]
[11, 22, 33, 44, 46, 47]
[11, 22, 33, 45, 46, 47]
[11, 22, 44, 45, 46, 47]
[11, 33, 44, 45, 46, 47]
[22, 33, 44, 45, 46, 47]

来自引用的 URL 的屏幕截图:

我将为您添加非常相同的方法的递归实现,该方法也将基数(子集长度)作为输入参数。

private static List<List<Integer>> bruteForceRecursive(List<Integer> numbers, List<Integer> indexes, int base) {
    List<List<Integer>> result = new ArrayList<>();

    if (indexes.size() == base) {
        List<Integer> list = new ArrayList<>();

        indexes.forEach(x -> list.add(numbers.get(x)));
        result.add(list);

        return result;
    }

    for (int i = indexes.isEmpty() ? 0 : indexes.get(indexes.size() - 1) + 1; i < numbers.size(); i++) {
        indexes.add(i);
        result.addAll(bruteForceRecursive(numbers, indexes, base));
        indexes.remove(indexes.size() - 1);
    }

    return result;
}

然后是用法示例:

public static void main(String[] args) {
    List<Integer> numbers;

    numbers = new ArrayList<Integer>() {{ add(11); add(22); add(33); add(44); add(45); add(46); add(47);}};

    bruteForceRecursive(numbers, new ArrayList<>(), 2).forEach(System.out::println);
    bruteForceRecursive(numbers, new ArrayList<>(), 4).forEach(System.out::println);
    bruteForceRecursive(numbers, new ArrayList<>(), 6).forEach(System.out::println);
}

打印出来的:

[11, 22]
[11, 33]
[11, 44]
[11, 45]
[11, 46]
[11, 47]
[22, 33]
[22, 44]
[22, 45]
[22, 46]
[22, 47]
[33, 44]
[33, 45]
[33, 46]
[33, 47]
[44, 45]
[44, 46]
[44, 47]
[45, 46]
[45, 47]
[46, 47]
[11, 22, 33, 44]
[11, 22, 33, 45]
[11, 22, 33, 46]
[11, 22, 33, 47]
[11, 22, 44, 45]
[11, 22, 44, 46]
[11, 22, 44, 47]
[11, 22, 45, 46]
[11, 22, 45, 47]
[11, 22, 46, 47]
[11, 33, 44, 45]
[11, 33, 44, 46]
[11, 33, 44, 47]
[11, 33, 45, 46]
[11, 33, 45, 47]
[11, 33, 46, 47]
[11, 44, 45, 46]
[11, 44, 45, 47]
[11, 44, 46, 47]
[11, 45, 46, 47]
[22, 33, 44, 45]
[22, 33, 44, 46]
[22, 33, 44, 47]
[22, 33, 45, 46]
[22, 33, 45, 47]
[22, 33, 46, 47]
[22, 44, 45, 46]
[22, 44, 45, 47]
[22, 44, 46, 47]
[22, 45, 46, 47]
[33, 44, 45, 46]
[33, 44, 45, 47]
[33, 44, 46, 47]
[33, 45, 46, 47]
[44, 45, 46, 47]
[11, 22, 33, 44, 45, 46]
[11, 22, 33, 44, 45, 47]
[11, 22, 33, 44, 46, 47]
[11, 22, 33, 45, 46, 47]
[11, 22, 44, 45, 46, 47]
[11, 33, 44, 45, 46, 47]
[22, 33, 44, 45, 46, 47]

【讨论】:

  • 非常感谢您的方法,它很有用,但也许我没有正确解释自己。好的,我很欣赏您的代码示例,但是我正在搜索的算法将生成 7、8、9、10 或 11 个数字的“倍数”。是的,倍数相当于 6 个数字的 7、28、84 等投注,但我不是在搜索 6 个数字的投注,而是在搜索 7、8、9、10 的“倍数”投注或 11 个数字,相当于您的函数返回的投注。
  • 例如,如果用户想用 20 个号码花费 500 欧元,程序将显示“倍数”中的选项,这些选项将平均使用这 20 个号码,或多或少均等,并且相当于 500 欧元(或更少)。例如,结果可能类似于:如果您想要 7 的倍数,则 500/7 = 71,因此程序将返回 7 的 71 倍数,这会将 20 个数字平均组合成 7 个数字的赌注。在内部,这 71 个数字的 7 倍数相当于 71x7 = 497 次投注。你现在明白我了吗?
  • 我认为您不了解要求。对于 20 个符号集,有 38760 种不同的组合,它们都是唯一的,它们都是由提供的代码生成的。这也是非开源算法的工作原理。
  • 如果您将输入 N(倍数基数)然后是 M 个符号(N 从包含 M 个符号的集合中创建长度为 N 的子集,而生成的结果小于 Q。
  • 我发布的蛮力算法可以很容易地重新设计为递归函数来累积任意长度的子列表 -> 不像现在这样 6。然后使用此算法从 20 个中生成 subsets of length 7,然后遍历它们以生成 subsets of length 6,而它们的计数小于 500 或 subsets of length 7 具有更多元素。如果subsets of length 7 都用完了,你还有一些钱可以花,那么还有不同的策略......
猜你喜欢
  • 1970-01-01
  • 2021-03-04
  • 1970-01-01
  • 2021-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-09
相关资源
最近更新 更多