开发者社区> 问答> 正文

程序无法正确计算概率

编辑:发现一个错误。错误的测试用例已更新

我有一个程序,其中掷出五个骰子,并为掷骰子分配了一只手。即什么都没有,一对,两对,三个,一个满屋子,四个,一个,五个。该代码将运行1000000次,并为每个掷骰子提供诱因机会。在下面,我找到了我的代码应该输出的大致百分比:

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

但是我的代码给出以下输出:

Case 1, None alike is 7.57E-4
Case 2, One pair is 0.019422
Case 3, Two pair is 0.270331
Case 4, Three of a kind is 0.153657
Case 5, Full House is 0.270331
Case 6, Four of a kind is 0.019422
Case 7, Five of a kind is7.57E-4

我不明白为什么我的课程比例这么低。他们甚至不加一。我已经通过并测试了我的逻辑,但是从我所看到的来看,这是合理的。我希望能有另一双眼睛看一下,并让我知道它们是否可以吸引人。下面是我的代码:

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];
            rolls[0] = (int)(1 + (Math.random()*(6)));
            rolls[1] = (int)(1 + (Math.random()*(6)));
            rolls[2] = (int)(1 + (Math.random()*(6)));
            rolls[3] = (int)(1 + (Math.random()*(6)));
            rolls[4] = (int)(1 + (Math.random()*(6)));
            int [] counts = Counts(rolls);
            int length = counts.length;
            int hand = 0;
            for (int j = 0; j < length; j++) {
                if (counts[j] == 5) {
                    hand = 7;
                } 
            }
            if (hand < 7) {
                for (int j = 0; j < length; j++) {
                    if (counts[j] == 4) {
                        hand = 6;
                    } 
                }
            }
            if (hand < 6) {
                int counter = 0;
                for (int j = 0; j < length; j++) {
                    if (counts[j] == 3) {
                        counter++;
                    }
                    if (counts [j] == 2) {
                        counter++;
                    } 
                }
                if (counter == 2) {
                    hand = 5;
                }
            }
            if (hand < 5) {
                for (int j = 0; j < length; j++) {
                    if (counts[j] == 3) {
                        hand = 4;
                    } 
                }
            }
            if (hand < 4) {
                int newcounter = 0;
                for (int j = 0; j < length; j++) {
                    if (counts[j] == 2) {
                       newcounter++;
                    }
                }
                    if (newcounter == 2) {
                        hand = 3;
                    }
            }
            if (hand < 3) {
                for (int j = 0; j < length; j++) {
                    if (counts[j] == 2) {
                        hand = 2;
                        }
                    }
                }
            if (hand < 2) {
                hand = 1;
            }   
            if (hand == 7) {
                fivep++;
            }
            if (hand == 6) {
                fourp++;
            }
            if (hand == 5) {
                full++;
            }
            if (hand == 4) {
                threep++;
            }
            if (hand == 3) {
                twop++;
            }
            if (hand == 2) {
                pair++;
            }
            if (hand == 1) {
                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 "+fivep);
        System.out.println("Case 2, One pair is "+fourp);
        System.out.println("Case 3, Two pair is "+full);
        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;
    }
}

展开
收起
垚tutu 2019-12-04 16:49:39 660 0
0 条回答
写回答
取消 提交回答
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
新量⼦⾰命与量⼦计算 立即下载
图计算优化技术探索 立即下载
图计算及其应用 立即下载