蒙蒂大厅模拟不能按预期运作

时间:2014-06-27 03:30:11

标签: java algorithm

    import java.util.Scanner;
    import static java.lang.System.*;
    import static java.lang.Math.*;
    import java.util.Random; 


    public class Montyhall 
   {
    public static void main(String[] args) 
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter number of attempts:");
        int attempts = keyboard.nextInt();/*Decide how many times you want this to
        run. The larger the number, the more realistic an answer you will get.*/

        int curtains[] = new int[3]; //like the game show, one of these will be a winner

        Random rand = new Random(); /*sets the methods up with the same random seed, which
        otherwise changes by the milisecond and could influence the response*/

        withoutswitch(curtains, attempts, rand);
        withswitch(curtains, attempts, rand);



    }

    public static void withoutswitch(int curtains[], int attempts, Random rand)
    {

        int nsCorrect=0;

        for(int x=0; x < attempts; x++)
        {
            int winner = rand.nextInt(3);//Sets the winner to 1, leaving the other two at 0.
            curtains[winner] = 1;        //It then checks to see if they are the same, 
            int guess = rand.nextInt(3); //a 1/3 chance, roughly.

            if(curtains[guess]==1){
                nsCorrect++;
            }
            curtains[0]=0;
            curtains[1]=0;
            curtains[2]=0;
        //the player never changes their decision, so it does not matter if a door is opened.
        }
        System.out.println("Number of successes with no switch: " + nsCorrect);


    }

    public static void withswitch(int curtains[], int attempts, Random rand)
    {

        int ysCorrect=0;
        int goat = 0;

        for(int x=0; x < attempts; x++)
        {
            int winner = rand.nextInt(3);
            curtains[winner] = 1;
            int guess = rand.nextInt(3);
            goat = rand.nextInt(3);//one of the doors is opened
            while(goat == winner || goat == guess)//the opened door is randomized until
                goat = rand.nextInt(3); //it isn't the guess or the winner.


            int guess2 = rand.nextInt(3);
            while(guess2 == guess || guess2 == goat)
                guess2 = rand.nextInt(3);//the second guess goes through a similar process

            if(curtains[guess2]==1){

                ysCorrect++;
                }
            curtains[0]=0;
            curtains[1]=0;
            curtains[2]=0;
        }
        System.out.println("Number of successes with a switch: " + ysCorrect);
        }   

}

对不起它有点乱,我试图在将近一年的间歇后重新编码。第一部分按预期运行,大约有1/3的成功率。然而,第二个应该给我一个2/3的机会,但我仍然可以获得与开关大致相同的数量,就像没有开关一样。我仔细查看了网站,并且大部分都发现了我不熟悉的Java之外的东西。 This一个非常相似,但似乎没有人真正帮助解决主要问题。

我能做些什么来使赔率更加逼真?关于清理代码的建议也将受到赞赏。

编辑:代码现在正常运行,我现在只是想减少它。

3 个答案:

答案 0 :(得分:2)

在方法withoutswitch中,您需要更改

if(guess==1)
  nsCorrect++;

if (curtains[guess] == 1)
  nsCorrect++;

withswitch方法相同。每次运行for循环后,您需要将curtains重置为0.否则之前的1将在那里,经过几次运行后,curtains将只包含1。< / p>

private static void resetCurtains(int[] curtains) {
  for (int i = 0; i < curtains.length; i++) {
    curtains[i] = 0;
  }
}

在for循环中每次运行后调用该方法。

此外,我建议使用{},即使这些陈述是1-liner:

if (curtrains[guess] == 1) {
  nsCorrect++;
}

答案 1 :(得分:2)

您的两种方法都无法按预期运行。

你的“无切换”方法只从0,1或2中选择一个随机数,并在其1时添加到计数器。换句话说,我可以简化你的“无切换”方法:

public static void withoutswitch(int attempts, Random rand) {
    int counter = 0;
    for (int i = 0; i < attempts; i++) {
        if (rand.nextInt(3) == 1) counter++;
    }
    System.out.println(counter);
}

你的“withswitch”方法,不管你信不信,做同样的事情。你开始猜测使用一个变量,然后你完全忽略它并将它做到第二个变量,并检查它是否为1.因此,它产生完全相同的结果。

您的两种方法都使用“窗帘”数组,但不正确。这个问题的关键是每次都把汽车放在一个随机的门后面,但是你永远不会把你的阵列设置为全零,所以,经过几次运行后,它变成了所有的阵列,这绝对不是什么你想要的。

这是一些帮助你入门的伪代码:

number of switch wins = 0
number of stay wins = 0
scan in the number of attempts
loop through each attempt:
    make an array {0, 0, 0}
    pick a random number (0, 1, or 2), and set that array index to 1 (winning index)
    pick a random number (0, 1, or 2), and set it to choice
    pick a random number (0, 1, or 2), and set it to the shown door
    loop while the door number isn't the choice number or the winning index:
        pick a random number (0, 1, or 2) and set it to the shown door
    increment stay wins if the choice is equal to the winning index
    increment switch wins if (3 - choice - showndoor) is equal to the winning index
print stay wins and switch wins

注意:最后一位逻辑确定了开关门的索引,因为你唯一可能的索引是0,1和2,(0 + 1 + 2 = 3),然后是3 - 你选择的门 - 你看到的门=最后一扇门。

希望这有帮助!

答案 2 :(得分:0)

我会备份并重组整个事情。游戏的一个对象,两个后代,其中一个只是选择一扇门,其中一个选择然后切换。

我没有完全遵循你的开关逻辑,但它肯定是错的。 Monty的逻辑是,如果奖品在玩家门后,你随机打开一个,否则你打开一个与山羊一起。只需要一个随机数。

同样,播放器切换。此时只有一个窗帘,不需要随机数。

粗略地抨击逻辑(不是Java,而不是整个程序):

MontyHaulGame
{
    int[] Curtains = new int[3];
    int Car = Random(3);
    int Guess;
    Pick();
    if (Guess == Car) Wins++;    
}

MontyHaulNoSwitch : MontyHaulGame
{
    Pick()
    {
        Guess = Random(3);
    }
}

MontyHaulSwitch : MontyHaulGame
{
    Pick()
    {
         Guess = Random(3);
         OpenOne();
         Switch();
    }

    OpenOne()
    {
         if Guess == Car then
              Repeat
                  Monty = Random(3);
              Until Monty != Guess;
         else 
             Monty = 1;
             While (Monty == Guess) || (Monty == Car)
                  Monty++;
    }

    Switch()
    {
        NewGuess = 1;
        While (NewGuess == Guess) || (NewGuess == Monty)
            NewGuess++;
        Guess == NewGuess;
    }
}