
Monty Hall simulator in C
By: a guest on Feb 21st, 2012 | syntax:
C | size: 0.56 KB | hits: 13 | expires: Never
#include <stdio.h>
#include <stdlib.h>
// Monty Hall problem simulator
int main(int argc, char* argv[]){
unsigned char prize_door; // Player always selects door #0 of 2
int score_stay = 0,
score_switch = 0;
unsigned long i = 1, limit = argc==2? atol(argv[1]) : 100;
srand(time(NULL));
while(++i < limit && i > 0)
if(prize_door = rand()%3) score_switch++;
else score_stay++;
printf("%d games\
\n%d\t won by staying\
\n%d\t won by switching\
\n\n", i, score_stay, score_switch);
return 0;
}