英文:
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'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("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;
}
}
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's been a minute since I'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't right).
**Solution:**
I updated the result to look like the following:
double result = 0;
for (int itr = numberOfSuccess; itr <= 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'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's program to calculate these numbers is an option, [Troll](http://hjemmesider.diku.dk/~torbenm/Troll/) might work for you.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论