Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 骰子概率,n骰子大于或等于z骰子总数中的m值_Java_Combinatorics - Fatal编程技术网

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

Java 骰子概率,n骰子大于或等于z骰子总数中的m值,java,combinatorics,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 s

我一直在研究一个小函数来计算当掷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;
    }
}
当我有2个骰子,并且我希望其中1个符合标准时,我的概率为零,而我的概率应该为1。此外,当掷两个骰子而不是一个骰子时,获得单个结果的概率更小(这是不对的)

解决方案:

我将结果更新为如下所示:

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

你需要计算你通过的次数超过你需要通过的次数。例如,在使用问号标记的情况下,两个骰子都会成功,这意味着您永远不会只获得一个成功,因此您的代码报告概率为0


如果使用其他人的程序来计算这些数字是一种选择,可能对你有用。

你需要计算你通过的次数超过你需要通过的次数。例如,在使用问号标记的情况下,两个骰子都会成功,这意味着您永远不会只获得一个成功,因此您的代码报告概率为0


如果使用其他人的程序来计算这些数字是一种选择,可能对您有用。

请阅读以了解堆栈溢出是如何工作的,并阅读如何提高问题的质量。然后,您的问题将包含您的源代码,这些源代码可以由其他人编译和测试。添加了helper方法后,应该能够复制、粘贴和编译。请阅读以了解堆栈溢出是如何工作的,并阅读如何提高问题的质量。然后你的问题包括你的源代码,它可以被其他人编译和测试。添加了helper方法,应该能够复制,粘贴,编译。我明白你的意思了。我正试图弄清楚这个等式应该是什么样子。我是否需要添加nCr,其中r=numberOfSuccess+x,其中x表示一个递增的数字,直到达到numOfDice为止?@SethHays是的,这样就可以了(int itr=numberOfSuccess;itr我明白你的意思。我正试着想一想这个等式应该是什么样子。我是否需要加上nCr,其中r=numberOfSuccess+x,其中x表示一个递增的数字,直到达到numOfDice为止?@SethHays是的,可以这样做。我为(int itr=numberOfSuccess;itr)添加了以下内容
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