Got an iPhone or iPad? We have a brand new Pastebin App for both devices, and it's totally free! Click here to download the new Pastebin App for iOS.
Guest

Monty Hall simulator in C

By: a guest on Feb 21st, 2012  |  syntax: C  |  size: 0.56 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse
Copied
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. // Monty Hall problem simulator
  5.  
  6. int main(int argc, char* argv[]){
  7.  
  8.   unsigned char prize_door; // Player always selects door #0 of 2
  9.   int score_stay = 0,
  10.     score_switch = 0;
  11.   unsigned long i = 1, limit = argc==2? atol(argv[1]) : 100;
  12.  
  13.   srand(time(NULL));
  14.  
  15.   while(++i < limit && i > 0)
  16.     if(prize_door = rand()%3) score_switch++;
  17.       else score_stay++;
  18.    
  19.   printf("%d games\
  20.    \n%d\t won by staying\
  21.    \n%d\t won by switching\
  22.    \n\n", i, score_stay, score_switch);
  23.  
  24.   return 0;
  25.  
  26. }