monty.c (1248B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <time.h> 4 5 int *simulation(int n) 6 { 7 int win_with_switch_cup = 0; 8 int win_with_stay = 0; 9 10 srand(time(NULL)); 11 12 int i = 0; 13 for (i = 0; i < n; i++) { 14 int cash = 0; 15 int choice = 0; 16 cash = rand()%3; 17 choice = rand()%3; 18 19 int flipped_over = 0; 20 21 int a = 0; 22 for (a = 0; a < 3; a++) { 23 if ((a != cash) && (a != choice)) { 24 flipped_over = a; 25 } 26 } 27 28 int switch_cup = 0; 29 switch_cup = rand()%(2); 30 if (switch_cup) { 31 for (a = 0; a < 3; a++) { 32 if (a != choice && a != flipped_over) { 33 choice = a; 34 break; 35 } 36 } 37 } 38 39 40 if (choice == cash) { 41 if (switch_cup) { 42 win_with_switch_cup++; 43 } else { 44 win_with_stay++; 45 } 46 } 47 } 48 49 int *wins = malloc(2 * sizeof(int)); 50 wins[0] = win_with_switch_cup; 51 wins[1] = win_with_stay; 52 53 return wins; 54 } 55 56 int main(void){ 57 int n = 10000; 58 59 int *w = simulation(n); 60 61 printf("(%d, %d)\n", w[0], w[1]); 62 63 free(w); 64 return 0; 65 } 66