-
Notifications
You must be signed in to change notification settings - Fork 0
/
monty-hall.c
44 lines (38 loc) · 1.08 KB
/
monty-hall.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
* Copyright (C) 2014 Tomasz Kramkowski <[email protected]>
*
* This program is free software. It is licensed under version 3 of the
* GNU General Public License.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see [http://www.gnu.org/licenses/].
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ITERATIONS 1000
int main(int argc, char **argv)
{
srand(time(NULL));
int i, won = 0, lost = 0, opt, winopt, discard;
for(i = 0; i<ITERATIONS; i++){
opt = (float)rand() / (float)RAND_MAX * 3.0;
winopt = (float)rand() / (float)RAND_MAX * 3.0;
if(opt == winopt){
discard = (float)rand() / (float)RAND_MAX * 2.0;
if(!opt)
discard +=1;
else if(opt == 1 && discard)
discard = 2;
}else{
discard = 3 - opt - winopt;
}
opt = 3 - opt - discard;
if(opt == winopt)
won++;
else
lost++;
}
printf("Won: %d, Lost: %d.\n", won, lost);
return 0;
}