-
Notifications
You must be signed in to change notification settings - Fork 0
/
Monty_Hall_Simulation.cpp
63 lines (55 loc) · 1.6 KB
/
Monty_Hall_Simulation.cpp
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
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#include<iomanip>
int main(int argc, const char * argv[])
{
freopen("Observations.txt","w",stdout);
int prize, door, goat, totalTrials;
double win = 0, loose = 0;
srand(time(NULL));
for(totalTrials = 10;totalTrials <= 1000000000;totalTrials *= 10)
{
win = 0;
loose = 0;
for(int i = 0;i < totalTrials;i++){
/*
below if we fix 'only' one of door or Prize then also we will get same result.
door=1;
prize=1;
*/
// Randomly choosing Gate and Prize value
prize = rand()%3;
door = rand()%3;
//one door is opened now which has Goat behind it.
for(int i = 0;i < 3;i++)
{
if(i != door&&i != prize)
{
goat=i;
break;
}
}
// and now we will change our door
for(int i = 0;i < 3;i++)
{
if(i != goat&&i != door)
{
door=i;
break;
}
}
if(door == prize)
{
win += 1;
}
else
{
loose += 1;
}
}
std::cout<<"for Total trials = "<<std::setfill('0')<<std::setw(10)<<totalTrials<<" Probability of winning if we switch our door is "<<" = "<<(double)win/totalTrials<<std::endl;
}
return 0;
}