java - 计算掷骰子游戏中 10,000 次模拟的获胜概率(赢/(赢 + 输))。这是作业的一部分

标签 java simulation probability

使用掷骰子游戏中的 10,000 次模拟来计算获胜概率 (wins/(wins + Loss))。下面是双骰子游戏的方法:

public class CrapsGame {

    public static void main(String[] args) {
            int dice1 = (int)(Math.random()* 6) + 1;
            int dice2 = (int)(Math.random()* 6) + 1;
            int roll = dice1 + dice2;
            System.out.println();
            System.out.print("You rolled "+roll+". ");
            if(roll == 2 || roll == 3 || roll == 12){
                    System.out.println("You Lose !");
            }else if(roll == 7 || roll == 11){
                    System.out.println("You Win !");
            }else{
                    System.out.println("Point is "+roll+"\n");
                    dice1 = (int)(Math.random()* 6) + 1;
                    dice2 = (int)(Math.random()* 6) + 1;
                    int roll2 = dice1 + dice2;
                    System.out.print("You rolled "+roll2+". ");
                    while(roll2 != 7){
                            if(roll == roll2){
                                    System.out.println("You Win !");
                                    break;
                            }else{
                                    System.out.println("Point is "+roll+"\n");
                            }
                            dice1 = (int)(Math.random()* 6) + 1;
                            dice2 = (int)(Math.random()* 6) + 1;
                            roll2 = dice1 + dice2;
                            System.out.print("You rolled "+roll2+". ");
                    }
                    if(roll2 == 7){
                            System.out.println("You Lose !");
                    }                       
            }
    }
}

我认为这应该不难,我只需要代码来运行 10,000 次模拟,然后计算概率。谢谢:)

是否可以让某人插入此内容的工作版本

在逻辑之外放置一个 while 或 for 循环并创建 2 个计数器(timesWinning、timesLosing)。根据现有代码内的情况递增每个值。循环运行 10.000 次后,根据需要进行数学计算:wins/(wins + Loss)

谢谢,这是作业的一部分

最佳答案

最后你应该将他的代码修改为:

 System.out.println("Probability of winning: " +  ((double)timesWon/(timesWon + timesLost)));

这是我自己的结果:

您玩了:10000.0,赢了:5078,赢了的概率:0.5078

您玩过:1.0E8,获胜:50707214,获胜概率:0.50707214

关于java - 计算掷骰子游戏中 10,000 次模拟的获胜概率(赢/(赢 + 输))。这是作业的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40956084/

相关文章:

java - hibernate中复合键的Where子句

Java:限制在小区域内的基于泊松的点过程

java - 使用相同的种子或相同的随机流初始化多个分布?

java - 为什么控制台窗口仍然输出 'UnhandledAlertException'?

java - 在 IntelliJ 中使用 Java 调试器时, "Drop Frame"是什么意思?

c++ - 计算 C++ 中是否有 1/4 机会发生某些事情的最佳方法?

java - 随着时间的推移概率地选择一组项目

algorithm - 水库采样问题

java - 请求转发后如何停止将 Portlet ImplicitModel 传播到下一个处理程序?

python - 如何为任务优先级建模?