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

dice probability, n dice greater than or equal to m value out of z total dice

我一直在研究一个小函数来计算在掷 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

您需要计算通过次数多于通过检查次数的次数。例如,在您用问号标记的情况下,两个骰子总是成功,这意味着您永远不会恰好获得一次成功,因此您的代码报告概率为 0。

如果可以选择使用其他人的程序来计算这些数字,Troll 可能适合您。