aboutsummaryrefslogtreecommitdiffstats
path: root/monty-hall.c
blob: 4db5bce9b5e68396e96a394fe12d169ede39c617 (plain)
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 <tk@the-tk.com>
 *
 * 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;
}