【问题标题】:Monty Hall Lets Make A Deal Java Simulation 10000 iterationsMonty Hall 让我们做一笔 Java 模拟 10000 次迭代
【发布时间】:2015-10-08 07:58:46
【问题描述】:

我要创建一个运行 Monty Hall 的程序,让我们进行 10000 次交易并输出以下统计数据:

  • 胜负数
  • 玩家切换并赢得与留下并保持的次数 获胜。
  • 显示切换和停留时的胜负百分比。

切换和获胜与保持和获胜的期望输出应该在切换时的 2/3 左右。我得到了,但只有一半;切换时为 33%,停留时为 16%。我不知道为什么其他 50% 没有出现。 很确定它与我的第二个 switch 语句有关,但无法弄清楚(可能是由于缺乏睡眠)。

我做错了什么?提前致谢。

                int iterations; 
                for (iterations = 0; iterations < 10000; iterations++)
                {
                    int prizeIs = (int) ((Math.random() * 3) + 1);  
                    int compChoice = (int) ((Math.random() * 3) + 1);
                    int zonkIs = 0;
                    if ( prizeIs == compChoice )
                    {
                            boolean chooseFirstZonk = Math.random() < 0.5; // 50% chance
                            switch ( prizeIs ) 
                            {
                                case 1: if ( chooseFirstZonk ) 
                                        {
                                            zonkIs = 2;
                                        }    
                                        else 
                                        {
                                            zonkIs = 3;
                                        }
                                        break;
                                case 2: if ( chooseFirstZonk ) 
                                        {
                                            zonkIs = 1;
                                        }    
                                        else 
                                        {
                                            zonkIs = 3;
                                        }
                                        break;
                                case 3: if ( chooseFirstZonk ) 
                                        {
                                            zonkIs = 1;
                                        } 
                                        else 
                                        {
                                            zonkIs = 2;
                                        }
                                        break;
                                }
                        }
                        else if (prizeIs == 1 && compChoice == 2)
                        {
                            zonkIs = 3; 
                        }
                        else if (prizeIs == 1 && compChoice == 3 )
                        {
                            zonkIs = 2; 
                        }
                        else if (prizeIs == 2 && compChoice == 1 )
                        {
                            zonkIs = 3;
                        }
                        else if (prizeIs == 2 && compChoice == 3 )
                        {
                            zonkIs = 1;
                        }
                        else if (prizeIs == 3 && compChoice == 1 )
                        {
                            zonkIs = 2;
                        }
                        else if (prizeIs == 3 && compChoice == 2 )
                        {
                            zonkIs = 1;
                        }

                         //generating a 1 or 2 to decide whether to switch doors or not
                        int switchDoor = (int) (1 +  (Math.random() * 2));

                        switch ( switchDoor ) 
                        {
                            //not switching doors 
                            case 1:  
                                    {
                                        //since we didnt switch 
                                        //if compchoice == prize 
                                        //then thats considered a win 
                                        //for not switching 
                                        if (compChoice == prizeIs)
                                        {
                                            noSwitch++;
                                            wins++;
                                            games++;
                                        }
                                        //if we didnt win 
                                        //the games will be incremented by 1
                                        //later to use to calculate the losses 
                                        else
                                        {
                                            games++;
                                        }
                                    }    
                                    break;
                            //switch door
                            case 2:
                                    {
                                        //since we did switch 
                                        //if compchoice == prize 
                                        //then thats considered a loss 
                                        //because were switching 
                                        //to the door that has a zonk 
                                        if (compChoice == prizeIs)
                                        {
                                            games++;
                                        }

                                        //if compchoice != prize 
                                        //then thats considered a win 
                                        //because were switching from the door 
                                        //with a zonk to the door with the prize 
                                        else if(compChoice != prizeIs)
                                        {

                                            switches++;
                                            wins++;
                                            games++;
                                        }

                                    }
                        }
                if (iterations == 10000)
                {

                    double percentage = 100.0 * (switches/games);
                    double noswitchpercentage = 100.0 *(noSwitch/games);
                    System.out.println( "Switch percentage : " + percentage);
                    System.out.println( "No Switch percentage : " + noswitchpercentage);
                    System.out.println( "wins : "+ wins);
                    System.out.println("losses : " + (games - wins));
                    break;
                }

【问题讨论】:

    标签: if-statement for-loop switch-statement logic probability


    【解决方案1】:

    当玩家获胜时,您只会增加 switch/noswitch。将 switch/noswitch 增量移到 if 之外,最好将游戏增量移到整个 switch 块之外,以免重复。

    ... 或者我不太明白这些变量的用途。因为这将为您带来 50% 的游戏。

    如果您真的想知道每个策略的胜率,您需要存储您使用每个策略的次数,以及该策略的胜率,这比您当前存储的要多一件事。然后计算是wins_with_strategy/plays_with_strategy,而不是除以总游戏数。

    【讨论】:

    • 我认为您指的是用于将 zonk 分配到正确门的开关。我添加了一些 cmets 来阐明我对第二个开关的想法。谢谢。
    • 开关没问题。变量的命名很有趣。如果您仔细观察,您会发现您正在计算通过切换变量switches 的获胜次数。然后计算switches/games,这意味着“在切换时赢得的游戏数除以无论是否切换的总游戏数” - 总游戏数可能约为 2*number选择了切换的游戏,这解释了你奇怪的半结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-09
    相关资源
    最近更新 更多