Problem Statement: The Monty Hall problem, also known as the "Three Doors Problem," is described as follows: There are three doors; behind two of them are goats, and behind one is a car. The contestant does not know what lies behind any of the doors. The contestant needs to pick one of the three doors, hoping it hides the car. The host knows what is behind each door. The host must open one of the remaining doors and must offer the contestant a chance to switch. The host will always pick a door with a goat behind it. If the contestant picked a door with a goat, the host must open the other door with a goat. If the contestant picked the door with the car, the host randomly opens one of the other two doors, both of which have goats. The contestant is then asked whether to stick with their original choice or switch to the remaining door. Does switching increase the contestant's chances?

C
#include <stdlib.h>
#include <time.h>
#include <stdio.h>

int test()
{
    int box = 3;
    int right ; //正确的
    int us; //用户选择
    int ch; // 改主意
    int drop; //丢弃的id
    int status ;
    right = rand() % 3;
    us = rand() % 3;
    ch = rand() % 2;
    drop = rand() % 2;
    if(right == us)
        drop = us > drop ? drop : drop+1;
    else
        drop = 3 - us - right;
    //printf("%d %d %d %d\n",right,drop,us,ch);
    ch  = 1; //强制改变
    if(ch) us = 3 - us - drop;
    status = (us == right);
    //printf("%d %d %d %d [%d]\n",right,drop,us,ch,status);
    return us == right;
}

int main()
{
    long all = 1000000000;
    long win = 0;
    double per ;
    srand((unsigned) time(NULL));
    for(long i=0;i<all;i++)
    {
        win += test();
        per = (double)win/(double)(i+1);
        //
        //printf("%f -- w:%ld a:%ld r",per,win,i+1);
        //getchar();
    }
    per = (double)win/(double)all ;
    printf("\n%f -- w:%ld a:%ld \n",per,win,all);
    return 0;
}

Execution Results:

Bash
$ gcc sim.c -o sim && ./sim

0.666623 -- w:666622815 a:1000000000
Of course, switch

Try it yourself