[英]Some test cases not running but some are in terms of probability
I have a program in which it rolls five dice and assigns a hand to the rolls.我有一个程序,它可以掷五个骰子并分配一只手来掷骰子。 ie nothing, a pair, two pair, three of a kind, full house, four of a kind, five of a kind.
即没有,一对,两对,三对,满屋,四对,五对。 The code runs 1000000 times and gives percentage chances for each roll.
该代码运行 1000000 次,并给出每次滚动的百分比机会。 Below I have attached general percentages my code should output near:
下面我附上了我的代码应该输出的一般百分比:
Case 1, None alike, is 0.092533
Case 2, One pair, is 0.462799
Case 3, Two pair, is 0.231789
Case 4, Three of a kind, is 0.154192
Case 5, Full house, is 0.038595
Case 6, Four of a kind, is 0.019316
Case 7, Five of a kind, is 0.000776
However my code gives the following output:但是我的代码给出了以下输出:
Case 1, None alike is 0.093099
Case 2, One pair is 0.384768
Case 3, Two pair is 0.076921
Case 4, Three of a kind is 0.15485
Case 5, Full House is 0.270349
Case 6, Four of a kind is 0.019281
Case 7, Five of a kind is7.33E-4
I don't understand why my programs percentages are off for one pair, two pairs, and full house.我不明白为什么我的节目百分比在一对、两对和满堂彩中有所下降。 I have gone through and tested my logic but it is sound from what I have seen.
我已经通过并测试了我的逻辑,但从我所看到的来看它是合理的。 Originally, my one pair was correct but my two pair was 0.0.
本来,我的一对是正确的,但我的两对是 0.0。 Below is my original logic which causes the two pair to be 0 and my pair to be correct.
下面是我的原始逻辑,它导致两对为 0,而我的对是正确的。
I, however, changed it to the current logic to get the current output.但是,我将其更改为当前逻辑以获取当前输出。 I would appreciate another set of eyes to take a look and let me know if they could catch something.
我会很感激另一双眼睛来看看,让我知道他们是否能捕捉到什么。 Below is my code:
下面是我的代码:
Change 1 :变化1:
if (hand < 6) {
int counter3 = 0;
int counter2 = 0;
for (int j = 0; j < length; j++) {
if (counts[j] == 3) {
counter3++;
}
if (counts [j] == 2) {
counter2++;
}
}
if (counter3 == 1 && counter2 == 1) {
hand = 5;
}
}
Change 2:变化2:
if (hand < 4) {
int newcounter = 0;
for (int j = 0; j < length; j++) {
if (counts[j] == 2) {
newcounter++;
}
}
if (newcounter==2) {
hand = 3;
}
if (newcounter == 1) { hand = 2; }
}
Change 3 :变化3:
Please remove if( hand < 3)
part of code.请删除
if( hand < 3)
部分代码。
Updated my answer.更新了我的答案。 In your code the
counter
variable (when you're trying to check "full house") was becoming 2 due to two pairs (ex : counts = 020200) not due to full house (ex: counts = 300200).在您的代码中,
counter
变量(当您尝试检查“满屋”时)由于两对(例如:counts = 020200)而不是由于满屋(例如:counts = 300200)而变为 2。 Hence, it wasn't counting the two pairs in the following code where it was supposed to because hand
was already becoming 5, so it didn't go inside any other if
parts below although it was supposed to go inside if(hand<3)
.因此,它没有计算下面代码中应该计算的两对,因为
hand
已经变成 5,所以它没有进入任何其他if
部分,尽管它应该进入if(hand<3)
。 Hope it will fix the issue.希望它能解决这个问题。
See comment in code查看代码中的注释
if (hand < 4) {
int newcounter = 0;
boolean firstp = false;
boolean secondp = false;
for (int j = 0; j < length; j++) {
firstp = false;
secondp = false;
if (counts[j] == 2) {
firstp = true; <---- THIS
}
if (counts[j] == 2) {
secondp = true; <---- AND THIS will always hit together as j never changes from the first if to second if
// break;
}
}
if (firstp && secondp) {
hand = 3; <---- firstp always equal to secondp, I would be surprised to see hand ever = 2
}
}
I modified your code.我修改了你的代码。 The original logic is a little bit messy.
原来的逻辑有点乱。 I made some slight improvement but hopefully better.
我做了一些轻微的改进,但希望更好。 Not perfect though.
虽然不完美。
import java.util.*;
public class PokerDice {
public static void main(String[] args) {
double none = 0;
double pair = 0;
double twop = 0;
double threep = 0;
double full = 0;
double fourp = 0;
double fivep = 0;
for (int i = 0; i<=1000000; i++) {
int [] rolls = new int[5];
for (int j = 0; j < 5; j++) {
rolls[j] = (int)(1 + (Math.random()*(6)));
}
int[] counts = Counts(rolls);
boolean has_two = false;
boolean has_three = false;
none++;
for (int j = 0; j < counts.length; j++) {
if (counts[j] == 4) {
fourp++;
none--;
break;
}
if (counts[j] == 5) {
fivep++;
none--;
break;
}
if (counts[j] == 3) {
has_three = true;
if (has_two) {
full++;
pair--;
break;
} else {
none--;
threep++;
}
}
if (counts[j] == 2) {
if (has_two) {
twop++;
pair--;
break;
}
else if (has_three) {
full++;
threep--;
break;
} else {
has_two = true;
pair++;
none--;
}
}
}
}
fivep/=1000000;
fourp/=1000000;
full/=1000000;
threep/=1000000;
twop/=1000000;
pair/=1000000;
none/=1000000;
System.out.println("Poker Dice Probability Calculator");
System.out.println("Running 1,000,000 trials");
System.out.println();
System.out.println("Case 1, None alike is "+none);
System.out.println("Case 2, One pair is "+pair);
System.out.println("Case 3, Two pair is "+twop);
System.out.println("Case 4, Three of a kind is "+threep);
System.out.println("Case 5, Full House is "+full);
System.out.println("Case 6, Four of a kind is "+fourp);
System.out.println("Case 7, Five of a kind is"+fivep);
}
public static int[] Counts (int [] rolled) {
int one = 0;
int two = 0;
int three = 0;
int four = 0;
int five = 0;
int six = 0;
int len = rolled.length;
int [] rolltimes = new int[6];
for (int i = 0; i<len; i++) {
if (rolled [i] == 1) {
one++;
}
else if (rolled [i] == 2) {
two++;
}
else if (rolled [i] == 3) {
three++;
}
else if (rolled [i] == 4) {
four++;
}
else if (rolled [i] == 5) {
five++;
}
else if (rolled [i] == 6) {
six++;
}
}
rolltimes[0] = one;
rolltimes[1] = two;
rolltimes[2] = three;
rolltimes[3] = four;
rolltimes[4] = five;
rolltimes[5] = six;
return rolltimes;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.