-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonty_hall_problem.cpp
172 lines (164 loc) · 4.02 KB
/
monty_hall_problem.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// monty-hall problem
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <random>
auto get_random_number_1_to_(int x) -> int {
typedef std::mt19937 rng_type;
std::uniform_int_distribution<rng_type::result_type> udist(0, 7);
rng_type rng;
// seed rng first:
//rng_type::result_type const seedval = get_seed(); // get this from somewhere
//rng.seed(seedval);
rng_type::result_type random_number = udist(rng);
return random_number;
}
auto monty_hall_problem(auto guess)
{
std::cout << "You guess was: " << guess << "\n";
auto where_goat_is = [](auto guess_) {
auto value = get_random_number_1_to_(3);
if (value == guess_) {
if ((value + 1) < 4) {
return value + 1;
} else {
// value + 1 == 4
return 1;
}
} else {
return value;
}
}(guess);
std::cout << "The goat is in position: " << where_goat_is << "\n";
auto where_car_is = [](auto where_goat_is_)
{
auto first_thought = get_random_number_1_to_(2);
if((where_goat_is_) == 1)
{
if(first_thought == 1)
{
return 2;
}
else
{
return 3;
}
}
else if((where_goat_is_) == 2)
{
if(first_thought == 1)
{
return 1;
}
else
{
return 3;
}
}
else if((where_goat_is_) == 3)
{
if(first_thought == 1)
{
return 1;
}
else{
return 2;
}
}
return 1;
}(where_goat_is);
std::cout << "Would you like to change your position: ";
bool would_change_position = []()
{
auto value = get_random_number_1_to_(2);
if(value == 1)
{
return false;
}
else
{
return true;
}
}();
std::cout << std::boolalpha << would_change_position;
if(would_change_position)
{
auto curr_position = 0;
if((guess == 1) && (where_goat_is == 2))
{
curr_position = 3;
}
else if((guess == 1) && (where_goat_is == 3))
{
curr_position = 2;
}
else if((guess == 2) && (where_goat_is == 1))
{
curr_position = 3;
}
else if((guess == 2) && (where_goat_is == 3))
{
curr_position = 1;
}
else if((guess == 3) && (where_goat_is == 1))
{
curr_position = 2;
}
else if((guess == 3) && (where_goat_is == 2))
{
curr_position = 1;
}
if(curr_position == where_car_is)
{
//std::cout << "It was better_to_change!\n";
return 1;
}
else
{
//std::cout << "In this case it was not better_to_change!\n";
return 0;
}
}
else
{
if(guess == where_car_is)
{
//std::cout << "In this case it was not better_to_change!\n";
return 0;
}
else
{
//std::cout << "It was better_to_change!\n";
return 1;
}
}
}
int main() {
int better_to_change = 0;
int better_to_stay = 0;
for(int i = 0; i < 10; i++)
{
auto random_num = get_random_number_1_to_(3);
auto x = monty_hall_problem(random_num);
if(x == 0)
{
better_to_stay += 1;
std::cout << "\t better to stay\n";
}
else if(x == 1)
{
better_to_change += 1;
std::cout << "\t better to change\n";
}
}
std::cout << "better_to_stay: " << better_to_stay << "\n";
std::cout << "better_to_change: " << better_to_change << "\n";
if(better_to_change > better_to_stay)
{
return better_to_change;
}
else
{
return better_to_stay;
}
}