-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulate.cpp
131 lines (108 loc) · 2.89 KB
/
simulate.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
/******************************************************************************
This simulates the Monty-Hall problem
Copyright (C) 2018 Kingshuk
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
******************************************************************************/
#include<iostream>
#include<chrono>
#include<random>
#include<vector>
#include<cstdlib>
#ifdef DEBUG_SIM
int debug= 1;
#else
int debug= 0;
#endif
class Random {
public:
inline Random()
: _gen(std::chrono::system_clock::now().time_since_epoch().count())
{
}
inline ~Random()
{
}
inline int get(const int min, const int max)
{
std::uniform_int_distribution<int> dist(min, max);
const int num= dist(_gen);
return num;
}
private:
std::default_random_engine _gen;
}; // class Random
Random _random= Random();
class Game {
public:
inline Game()
: _goodie(-1)
{
}
inline ~Game()
{
}
inline void play()
{
_goodie= _random.get(0, 2);
if(debug) {
std::cout<< "good= "<< _goodie<< ", ";
}
}
inline int openEmptyGate(const int firstChoice)
{
if(debug) {
std::cout<< "choice= "<< firstChoice<< ", ";
}
int empty;
if(_goodie== firstChoice) {
empty= (_goodie+ _random.get(1, 2))% 3;
} else {
empty= 3- (_goodie+ firstChoice);
}
if(debug) {
std::cout<< "empty= "<< empty<< ", ";
}
return empty;
}
inline bool check(const int choice)
{
return _goodie== choice;
}
private:
int _goodie;
}; // class Game
int main(int argc, char *argv[])
{
int num;
if(argc< 2|| 0== (num= atoi(argv[1]))) {
num= 100;
}
Game game;
std::vector<int> metric(2, 0);
for(int i= 0; i< num; ++i) {
game.play();
int choice= _random.get(0, 2);
const int empty= game.openEmptyGate(choice);
const int change= _random.get(0, 1);
if(change) {
choice= 3- (choice+ empty);
}
const int isWin= game.check(choice)? 1: 0;
if(debug) {
std::cout<< "change= "<< change<< ", result= "<< isWin<< std::endl;
}
metric[change]+= isWin;
}
std::cout<< "no-change and win= "<< metric[0]<< std::endl;
std::cout<< " change and win= "<< metric[1]<< std::endl;
return 0;
}