蒙蒂霍尔项目

时间:2013-09-25 06:58:59

标签: c++

我目前正在编写C ++ Monty Hall问题模拟,并遇到了一些麻烦。我一直得到的错误是:

source.cpp(23): error C4700: uninitialized local variable 'doorReveal' used
source.cpp(25): error C4700: uninitialized local variable 'doorSwitch' used
source.cpp(52): error C4700: uninitialized local variable 'stayWin' used
source.cpp(56): error C4700: uninitialized local variable 'switchWin' used

我似乎无法弄清楚出了什么问题。该项目应该通过首先在前100次尝试中保持原始门选择然后在接下来的100次尝试中显示门时切换来模拟胜利。感谢大家的帮助。

 #include <iostream>
 #include <time.h>
 using namespace std;

 int main()
 {
     int doorChoice;
     int prizeDoor;
     int doorReveal;
     int doorSwitch;
     int count;
     int switchWin;
     int stayWin;

     srand((unsigned int) time(0));

     for (count = 0; count <= 200; count++)
     {
         prizeDoor = (rand() % 3) + 1;
         doorChoice = (rand() % 3) + 1;
         cout << "The prize door is door number " << prizeDoor << " ." << endl;
         cout << "The door the contestant chose was door " << doorChoice << endl;
         doorReveal != prizeDoor || doorChoice;
         cout << "The host revealed door number " << doorReveal << " ." << endl;
         doorSwitch != doorChoice || doorReveal;

         while (count < 101)
         {
             if (doorChoice == prizeDoor)
             {
                 cout << "Great Job! You won!" << endl;
             }
             else {
                 cout << "Not this time!" << endl;
             }
         }

         while (count < 201)
         {
             if (doorSwitch == prizeDoor)
             {
                 cout << "You switched and won!" << endl;
             }
             else {
                 cout << "You switched and lost!" << endl;
             }
         }

         if (doorChoice == prizeDoor)
         {
             stayWin++;
         }
         if (doorSwitch == prizeDoor)
         {
             switchWin++;
         }
         count++;
     }
     cout << "Your win percentage when staying was " << stayWin << "%!" << endl;
     cout << "Your win percentage when switching was " << switchWin << "%!" << endl;
     return 0;
 }

1 个答案:

答案 0 :(得分:10)

问题1:operator!=

operator !=没有按照您的想法行事。

你的意思是

    doorReveal = !(prizeDoor || doorChoice);
    cout << "The host revealed door number " << doorReveal << " ." << endl;
    doorSwitch = (doorChoice || doorReveal);

我在确定doorReveal时感觉到另一个逻辑问题。我稍后会考虑这个。 编辑:见问题5

问题2:while

while循环也存在问题:

    while(count < 101)

    // ...

    while(count < 201)

它们是无限循环,因为循环期间计数不会增加。我认为你的意思是if,而不是while

问题3:初始化switchWinstayWin

这些变量仅递增。就像@KonradRudolph建议的那样,

  • 将变量声明为首次需要
  • 初始化它们
  • 当你在它时,请在适当的时候标记const

问题4:rand()%3有偏见

你应该使用统一的发行版。

问题5:修正你的门'推导'

布尔人不是集合。即使它们是,你也会被困在二进制集中。我提出以下模型:

enum doors { door1 = 1, door2 = 2, door3 = 4, any = door1|door2|door3 };

所以你可以说:

doors const doorReveal = doors(~(prizeDoor | doorChoice)  & any);
doors const doorSwitch = doors(~(doorChoice | doorReveal) & any);

修复导致程序看起来有效:

#include <iostream>
#include <time.h>
using namespace std;

enum doors { door1 = 1, door2 = 2, door3 = 4, any = door1|door2|door3 };

static inline std::ostream& operator<<(std::ostream& os, doors val) {
    switch(val) {
    case door1: return os << "door #1";
    case door2: return os << "door #2";
    case door3: return os << "door #3";
    case any:   return os << "any door";
    }
    return os << "OOPS";
}

int main()
{
    unsigned switchWin = 0;
    unsigned stayWin   = 0;
    srand((unsigned int) time(0));
    for(int count = 0; count <= 200; count++)
    {
        doors const prizeDoor  = doors(1 << rand() / ( RAND_MAX / 3 ));
        doors const doorChoice = doors(1 << rand() / ( RAND_MAX / 3 )); 
        cout << "The prize door is door number " << prizeDoor << " ." << endl;
        cout << "The door the contestant chose was door " << doorChoice << endl;
        doors const doorReveal = doors(~(prizeDoor | doorChoice)  & any);
        doors const doorSwitch = doors(~(doorChoice | doorReveal) & any);

        cout << "The host revealed door number " << doorReveal << " ." << endl;
        if(count < 101)
        {
            if(doorChoice == prizeDoor)
            {
                cout << "Great Job! You won!" << endl;
            }
            else
            {
                cout << "Not this time!" << endl;
            }
        };
        if(count < 201)
        {
            if(doorSwitch == prizeDoor)
            {
                cout << "You switched and won!" << endl;
            }
            else
            {
                cout << "You switched and lost!" << endl;
            }
        };
        if(doorChoice == prizeDoor)
        {
            stayWin++;
        }
        if(doorSwitch == prizeDoor)
        {
            switchWin++;
        };
        count++;
    }
    cout << "Your win percentage when staying was " << stayWin << "%!" << endl;
    cout << "Your win percentage when switching was " << switchWin << "%!" << endl;
    return 0;
}