disable recursion with a -
[tridge/junkcode.git] / goats.c
1 static int doors[3];
2 static int choice1, choice2;
3 static int revealed;
4 static int trials[3], points[3];
5 static int strategy;
6
7 static void init_doors(void)
8 {
9         int prize;
10         int i;
11
12         for (i=0;i<3;i++) doors[i] = 0;
13         prize = random() % 3;
14         doors[prize] = 1;
15 }
16
17 static void choose1(void)
18 {
19         choice1 = random() % 3;
20 }
21
22 static void reveal(void)
23 {
24         do {
25                 revealed = random() % 3;
26         } while (revealed == choice1 || doors[revealed] == 1);
27 }
28
29 static void choose2(void)
30 {
31         switch (strategy) {
32         case 0:
33                 choice2 == choice1;
34                 break;
35         case 1:
36                 for (choice2 = 0; choice2 < 3; choice2++) {
37                         if (choice2 != choice1 && choice2 != revealed) break;
38                 }
39                 break;
40         case 2:
41                 do {
42                         choice2 = random() % 3;
43                 } while (revealed == choice2);
44                 break;
45         }
46 }
47
48 static void score(void)
49 {
50         trials[strategy]++;
51         points[strategy] += doors[choice2];
52 }
53
54 int main(int argc, char *argv[])
55 {
56         int i;
57
58         srandom(getpid());
59
60         while (trials[0] < 100000) {
61                 strategy = random() % 3;
62
63                 init_doors();
64
65                 choose1();
66                 reveal();
67                 choose2();
68                 score();
69         }
70
71         for (i=0;i<3;i++) {
72                 printf("strategy %d scored %d%%\n", i, (int)(0.5 + 100*points[i]/trials[i]));
73         }
74         return 0;
75 }