-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3doorsgame_10000iterations.c
69 lines (63 loc) · 2.05 KB
/
3doorsgame_10000iterations.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/******************************************************************************
Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <stdlib.h>
#include <stdio.h>
int randomize (int maxVal)
{
return (rand () % (int) (maxVal)) + 1;
}
int main ()
{
int seed = time (NULL);
srand (seed);
int doorNumber, i, doorwith_car, door_chosen,success;
printf ("Enter Number of Doors\n");
scanf ("%d", &doorNumber);
int iterations = 10000;
float success_rate;
int j;
int doors[doorNumber],goat_counter;
//char a;
for(j = 0,success = 0; j < iterations; j++)
{
doorwith_car = randomize (doorNumber);
door_chosen = randomize (doorNumber);
printf ("Chosen door is: %d, and there is a door with a car behind\n", door_chosen);
for (i = 0, goat_counter = 0; i < doorNumber; i++)
{
if (i != (doorwith_car - 1) && i != (door_chosen - 1))
{
if(goat_counter == (doorNumber -2))
break;
printf ("We had a goat behind the door %d\n", (i + 1));
goat_counter++;
}
}
if(doorwith_car != door_chosen)
success++;
}
success_rate = (1.0*success) / (1.0*iterations);
printf("Success rate is %f\n",success_rate);
/*
printf("Now the question is do you want to change the door or not?(Y/y or N/n)\n");
scanf (" %c", &a);
if (a == 'N' || a == 'n')
{
if (door_chosen == doorwith_car)
printf ("Congrats! You have won a car!!!\n");
else
printf ("Sorry, maybe next time\n");
}
else if (a == 'y' || a == 'Y')
{
if (door_chosen != doorwith_car)
printf ("Congrats! You have won a car!!!\n");
else
printf ("Sorry, maybe next time\n");
}
*/
return 0;
}