-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
44 lines (32 loc) · 872 Bytes
/
main.c
File metadata and controls
44 lines (32 loc) · 872 Bytes
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
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main() {
srand(time(NULL));
//Request values
int trials = 1000000;
int doors = 3;
printf("Enter amount of trials and doors (trials doors): ");
scanf("%d %d", &trials, &doors);
printf("\nTrials: %d\n", trials);
printf("Doors: %d\n", doors);
//Counters
int stayWins = 0;
int switchWins = 0;
//Dont allocate in the loop
int doorWithCar;
int initGuess;
int didGuess;
for (int i = 0; i < trials; ++i){
doorWithCar = rand() % doors;
initGuess = rand() % doors;
didGuess = initGuess == doorWithCar;
//Stay behavior
stayWins += didGuess;
//Switch behavior
switchWins += !didGuess;
}
printf("\nWithout switching, win rate is %f\n", (float)stayWins / trials);
printf("With switching, win rate is %f\n", (float)switchWins / trials);
return 0;
}