骰子概率,z个骰子中n个骰子的值大于或等于m

时间:2020-10-11 07:39:17

标签: java combinatorics

我一直在研究一个小的函数,用于计算滚动n个不同骰子时m值大于或等于x的概率。到目前为止,我有

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;

class DiceProbabilityCalculator {
    private static DecimalFormat df = new DecimalFormat("0.0000");

    public static void main(String[] args) {
        List<Integer> dice = Arrays.asList(1, 2, 3, 4, 5, 6);

        int numberOfDice = 2;
        int numberOfSuccess = 1;
        for (int check = 1, max = dice.size(); check <= max; ++ check) {
            int pass = max - check + 1;
            int failure = check - 1;

            double probPass = prob(pass, max);
            double probFail = prob(failure, max);
            double result = choose(numberOfDice, numberOfSuccess) * Math.pow(probPass, numberOfSuccess) * Math.pow(probFail, numberOfDice - numberOfSuccess);

            System.out.println(
                    "dice count: " + numberOfDice +
                    ", dice equal or greater than threshold: " + numberOfSuccess +
                    ", success threshold: " + check +
                    ", result: " + df.format(result)
            );
        }
    }

    static double prob(int countValue, int maxValue) {
        return (1.0 * countValue)/(1.0 * maxValue);
    }

    static double choose(int n, int k) {
        return (factorial(n) / (factorial(n-k)*factorial(k)));
    }

    static double factorial(int num) {
        if (num >= 1)
            return num * factorial(num - 1);
        else
            return 1;
    }
}

对于骰子数与成功数匹配的情况,结果似乎是正确的,但是如果成功数较少,我得到的数字将无效。

距离我上过任何统计课已经一分钟了,我不确定自己会忘记什么。

结果为:

dice count: 2, dice equal or greater than threshold: 1, success threshold: 1, result: 0.0000
dice count: 2, dice equal or greater than threshold: 1, success threshold: 2, result: 0.2778
dice count: 2, dice equal or greater than threshold: 1, success threshold: 3, result: 0.4444
dice count: 2, dice equal or greater than threshold: 1, success threshold: 4, result: 0.5000
dice count: 2, dice equal or greater than threshold: 1, success threshold: 5, result: 0.4444
dice count: 2, dice equal or greater than threshold: 1, success threshold: 6, result: 0.2778

当我有2个骰子,并且希望其中1个符合标准时,当我应该具有1的概率时,我的概率为零。另外,当掷两个骰子时,获得单个结果的概率也较小之一(不正确)。

解决方案:

我将结果更新如下:

double result = 0;
for (int itr = numberOfSuccess; itr <= numberOfDice; ++itr) {
    result += choose(numberOfDice, itr) * pow(probPass, itr) * pow(probFail, numberOfDice - itr);
}

这给了我看起来更正确的结果。

dice count: 2, dice equal or greater than threshold: 1, success threshold: 1, result: 1.0000
dice count: 2, dice equal or greater than threshold: 1, success threshold: 2, result: 0.9722
dice count: 2, dice equal or greater than threshold: 1, success threshold: 3, result: 0.8889
dice count: 2, dice equal or greater than threshold: 1, success threshold: 4, result: 0.7500
dice count: 2, dice equal or greater than threshold: 1, success threshold: 5, result: 0.5556
dice count: 2, dice equal or greater than threshold: 1, success threshold: 6, result: 0.3056

1 个答案:

答案 0 :(得分:0)

您需要计算出通过比通过支票更多的情况。例如,在标记了问号的情况下,两个骰子总是成功,这意味着您永远不会获得一个成功,因此您的代码报告的概率为0。

如果可以选择使用别人的程序来计算这些数字,那么Troll可能对您有用。