骰子概率,n枚骰子中大于或等于m值的骰子数,总共投掷z枚骰子。

huangapple go评论72阅读模式
英文:

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

问题

以下是您要翻译的代码部分:

我一直在编写一个小函数用于计算投掷n个不同骰子时获得大于或等于x的m个值的概率到目前为止我有以下代码

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;
    }
}

解决方案如下:

我更新了结果如下

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


<details>
<summary>英文:</summary>
I&#39;ve been working on a small function to calculate the probabilities of getting m values greater than or equal to x when rolling n different dice.  So far I have 
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;
class DiceProbabilityCalculator {
private static DecimalFormat df = new DecimalFormat(&quot;0.0000&quot;);
public static void main(String[] args) {
List&lt;Integer&gt; dice = Arrays.asList(1, 2, 3, 4, 5, 6);
int numberOfDice = 2;
int numberOfSuccess = 1;
for (int check = 1, max = dice.size(); check &lt;= 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(
&quot;dice count: &quot; + numberOfDice +
&quot;, dice equal or greater than threshold: &quot; + numberOfSuccess +
&quot;, success threshold: &quot; + check +
&quot;, result: &quot; + 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 &gt;= 1)
return num * factorial(num - 1);
else
return 1;
}
}
The result seems correct for the case where the number of dice match the number of successes, but if there are less successes, I am getting an invalid number.  
It&#39;s been a minute since I&#39;ve taken any statistics classes and I am not sure what I am forgetting.  
The results for the are:
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
When I have 2 dice, and am want 1 of them to meet the criteria, I get a probability of zero when I should have a probability of 1.  Also the probability of getting a single result is smaller when rolling two dice instead of one (that isn&#39;t right).
**Solution:**  
I updated the result to look like the following:
double result = 0;
for (int itr = numberOfSuccess; itr &lt;= numberOfDice; ++itr) {
result += choose(numberOfDice, itr) * pow(probPass, itr) * pow(probFail, numberOfDice - itr);
}
this gave me results that looked more correct.
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
</details>
# 答案1
**得分**: 0
你需要计算在击败检查所需次数之前获得的成功次数。在你用问号标记的情况下,例如,两个骰子总是成功的,这意味着你永远不会获得正好一次成功,因此你的代码报告的概率为0。
如果可以使用他人的程序来计算这些数字,[Troll](http://hjemmesider.diku.dk/~torbenm/Troll/) 可能适合你。
<details>
<summary>英文:</summary>
You need to count the cases where you get more passes than you need to beat the check. In the case that you&#39;ve marked with the question marks, for example, both dice always succeed, which means that you never get exactly one success, hence your code reports probability 0.
If using someone else&#39;s program to calculate these numbers is an option, [Troll](http://hjemmesider.diku.dk/~torbenm/Troll/) might work for you.
</details>

huangapple
  • 本文由 发表于 2020年10月11日 15:39:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/64301685.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定