开发者社区 问答 正文

程序无法正确计算概率

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

我有一个程序,其中掷出五个骰子,并为掷骰子分配了一只手。即什么都没有,一对,两对,三个,一个满屋子,四个,一个,五个。该代码将运行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 677 分享 版权
阿里云 AI 助理回答

从你的描述来看,你正在尝试模拟掷五颗骰子并统计各种组合出现的频率,并且期望这些频率接近于理论概率。然而,你的代码输出结果与预期有较大偏差,特别是满堂彩(Full House)的概率异常高,且一对、两对、四条的概率也与预期不符。

首先,确保你的逻辑正确无误地计算了每种情况。对于五颗骰子来说,正确的计数逻辑相当关键,尤其是处理像一对、两对、满堂彩这类复杂组合时。这里没有提供具体的逻辑实现部分,但基于你的问题,我建议检查以下几个方面:

  1. 确保随机性:确认生成骰子点数的方法是真正随机的。通常使用Random类的nextInt(6) + 1来模拟一个1到6的骰子。

  2. 组合计数逻辑

    • None alike (全不相同): 所有骰子点数都不相同。
    • One pair (一对): 只有两个骰子点数相同,其余三个各不相同。
    • Two pair (两对): 两个骰子点数相同,另外两个骰子点数相同,最后一个不同。
    • Three of a kind (三条): 三个骰子点数相同,其余两个各不相同。
    • Full house (满堂彩): 三个骰子点数相同,另外两个骰子点数相同。
    • Four of a kind (四条): 四个骰子点数相同,另一个不同。
    • Five of a kind (同花顺): 所有骰子点数都相同。
  3. 计数和比例计算:确保在循环结束后,将每个计数除以总次数(1000000次),以得到百分比。

  4. 代码审查:由于具体实现未给出,可能存在逻辑错误或遗漏的地方。例如,满堂彩的计算可能错误地包含了其他情况,导致其计数过高。

如果你能提供具体的实现逻辑代码段,我可以更准确地帮助你定位问题所在。不过,基于你的描述,重点应该放在检查和验证组合计数的逻辑上,确保它们严格符合定义。

有帮助
无帮助
AI 助理回答生成答案可能存在不准确,仅供参考
0 条回答
写回答
取消 提交回答
问答分类:
问答地址: