天天看點

用C++驗證三門問題

三門問題(換門):

#include <iostream>

#include <cstdlib>

#include <ctime>

#define random(a,b) (rand() % (b-a+1))+ a

using namespace std;

int main()

{

    srand((int)time(0));

    int doors[3];     

    int mode;

    int choose;

    int result; 

    int success = 0;

    for(int i = 0;i < 10000;++i) {//模拟一萬次

        mode = random(1,3);//3門問題的排列組合總共有三種情況

        if(mode == 1) {//模式一是前兩個門是羊,後一個門是汽車

            doors[0] = 1;

            doors[1] = 1;

            doors[2] = 2;

            choose = random(0,2);//玩家随機選擇其中一個門

            if(choose == 0) {//根據規則得到結果

                result = 2;

            }

            else if(choose == 1) {

                result = 2;

            }

            else {

                result = 0;

            }

            if(doors[result] == 2) {//判斷門後是否為汽車,如果為汽車,則統計量加1

                ++success;

            }

        }

        else if(mode == 2) {//模式二為車、羊、羊

            doors[0] = 2;

            doors[1] = 1;

            doors[2] = 1;

            choose = random(0,2);

            if(choose == 0) {

                result = 1;

            }

            else if(choose == 1) {

                result = 0;

            }

            else {

                result = 0;

            }

            if(doors[result] == 2) {

                ++success;

            }

        }

        else {//模式三為羊、車、羊

            doors[0] = 1;

            doors[1] = 2;

            doors[2] = 1;

            choose = random(0,2);

            if(choose == 0) {

                result = 1;

            }

            else if(choose == 1) {

                result = 0;

            }

            else {

                result = 1;

            }

            if(doors[result] == 2) {

                ++success;

            }

        }

    }

    cout << success << endl;//輸出成功的總數

    return 0;

}

三門問題(不換門):

#include <iostream>

#include <cstdlib>

#include <ctime>

#define random(a,b) (rand() % (b-a+1))+ a

using namespace std;

void game(int &success);//模拟做出選擇後不更改的函數

int doors[3];

int main()

{

    srand((int)time(0));

    int mode;

    int choose;

    int result;

    int success = 0;

    for(int i = 0;i < 10000;++i) {//實驗一萬次

        mode = random(1,3);//3門問題的排列組合總共有三種情況

        if(mode == 1) {//模式一是前兩個門是羊,後一個門是汽車

            doors[0] = 1;

            doors[1] = 1;

            doors[2] = 2;

            game(success);

        }

        else if(mode == 2) {//模式二為車、羊、羊

            doors[0] = 2;

            doors[1] = 1;

            doors[2] = 1;

            game(success);

        }

        else {//模式三為羊、車、羊

            doors[0] = 1;

            doors[1] = 2;

            doors[2] = 1;

            game(success);

        }

    }

    cout << success << endl;//輸出成功的總數

    return 0;

}

void game(int &success) {

    int result;

    result = random(0,2);//随機選擇一扇門

    if(doors[result] == 2) {//如果選對了,統計量+1

        ++success;

    }

}

結果:

換門後成功拿到汽車的頻率10組資料如下,都接近三分之二(0.6666)

0.6692

0.6717

0.6697

0.669

0.6616

0.6712

0.6663

0.6693

0.6656

0.6686

不換門成功拿到汽車的頻率10組資料如下,都接近三分之一(0.3333)

0.3409

0.3325

0.3399

0.3388

0.3354

0.3289

0.3337

0.3337

0.3343

0.3342

由此可見,三門問題的結論是正确的。