Advertisement
metalx1000

The Monty Hall Problem

Apr 30th, 2013
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.05 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. typedef char * string;
  4.  
  5. int main(void)
  6. {
  7.  
  8.     int st, sw, i, x;
  9.     char *p, *a, *b, *s;
  10.     st = 0; //count the number of wins with staying
  11.     sw = 0; //count the number of wins with switching
  12.  
  13.     string door[3];//create 3 doors (4 really)
  14.  
  15.     srand(time(NULL));//required for random numbers
  16.     printf("Stay Wins -- Switch Wins\n");
  17.  
  18.     //loop 100 times
  19.     for(i=0;i<100;i++){
  20.         //assign 3 door values
  21.         door[1] = "win";
  22.         door[2] = "lose";
  23.         door[3] = "lose";
  24.  
  25.         x=rand() % 3 + 1; //pick a random door
  26.         p = door[x]; //remember player's choice
  27.         if(x==1){
  28.             s="lose"; //if player picked door 1 switching loses
  29.             st++;  //log stay win
  30.         }
  31.         else{
  32.             s="win"; //if player picked door 1 switching wins
  33.             sw++;  //log switch win
  34.         }
  35.         printf("   %s   --   %s   \n", p, s);
  36.     }
  37.     printf("Staying will win %i of the time.\n", st);
  38.     printf("Switching will win %i of the time.\n", sw);
  39.     return 0;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement