Monty Hall problem: Difference between revisions

m (syntax highlighting fixup automation)
Line 676:
<syntaxhighlight lang="c">//Evidence of the Monty Hall solution.
 
#include <stdlib.h>
#include <stdio.h>
#include <stdlibstdbool.h>
#include <time.h>
#include <math.h>
#define NumSim 10000000
 
intvoid main(void) {
#define GAMES 3000000
unsigned long int i,stay=0;
 
int ChosenDoor,WinningDoor;
int main(void){
unsigned i, j, k, choice, winsbyswitch=0,bool door[3]={0,0,0};
 
srand(time(NULL)); //initialize random seed.
for(i=0; i<GAMES=NumSim; i++){
door[0] = (!(rand()%2)) ? 1: 0; //give door 1 either a car or a goat randomly.
if(door[0]) door[1]=door[2]=0; //if 1st door has car, give other doors goats.
WinningDoor=rand() % 3; // choosing winning door.
else{ door[1] = (!(rand()%2)) ? 1: 0; door[2] = (!door[1]) ? 1: 0; } //else, give 2nd door car or goat, give 3rd door what's left.
choice = rand()%3; //choose a random door.
ChosenDoor=rand() % 3; // selected door.
 
//if the next door has a goat, and the following door has a car, or vice versa, you'd win if you switch.
if(door[WinningDoor]=true,door[ChosenDoor])stay++;
if(((!(door[((choice+1)%3)])) && (door[((choice+2)%3)])) || (!(door[((choice+2)%3)]) && (door[((choice+1)%3)]))) winsbyswitch++;
}
door[WinningDoor]=false;
printf("\nAfter %u games, I won %u by switching. That is %f%%. ", GAMES, winsbyswitch, (float)winsbyswitch*100.0/(float)i);
}
}
printf("\nAfter %ulu games, I won %u by switchingstaying. That is %f%%. and I won by switching %lu That is %f%%", GAMESNumSim, winsbyswitchstay, (float)winsbyswitchstay*100.0/(float)i,abs(NumSim-stay),100-(float)stay*100.0/(float)i);
}
</syntaxhighlight>
 
Output of one run:
 
<pre> After 300000010000000 games, I won 19997473330728 by switchingstaying.That is 33.307277%. And I won by switching 6669272 That is 66.658233692723%. </pre>
 
=={{header|C sharp|C#}}==
7

edits