Monty Hall Calculations

So I'm having a bit of an issue when I'm trying to calculate the probability of each strategy in the Monty Hall problem. I'm trying to print out each game won, and the win/loss ratio for each method.

When I run the program, all of my variables read out as 0. I'm not sure where I made the error, if it was in the functions or in the math, but I'm a bit stuck.

By the way, I can't change my RNG setup ( Professor's rule :/ ).

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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
  #include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;

// Function Prototypes
int DrawNum (int);

int Stay(int, int, int);    // Stay strategy
int Switch(int, int, int);  // Switch strategy
int Guess(int, int, int);   // 50-50 strategy

int main()
{
    // Variables
    unsigned seed;
    int goat1, goat2;       // The two goats behind the doors
    double staytotal = 0;      // Holds the total amount of successes for each method
    double switchtotal = 0;
    double guesstotal = 0;
    double stayratio = 0;   // Hold the Win/Loss ratio for each method
    double switchratio = 0;
    double guessratio = 0;
    int temp = 0;
    const int MAX = 3;

    // Initialize RNG
    seed = static_cast<unsigned>(time(NULL));
    srand (seed);

    // Herding the goats...
    goat1 = DrawNum(MAX);
    goat2 = DrawNum(MAX);
    while (goat1 != goat2)
    {
        goat2 = DrawNum(MAX);
    }

    // Picking A door...
    int choice;
    choice = DrawNum(MAX);
    while (choice != goat1)
    {
        choice = DrawNum(MAX);
    }

    // 1,000 Attempts of Stay Method
    cout << "-------------------------- The Stay Method --------------------------" << endl;
    cout << "The 'Stay' Method is when the contestant stays with their choice even\n"
         << "after the host reveals the first goat. For example, if the contestant\n"
         << "picks Door #1, and the host reveals a goat in Door #2, the contestant\n"
         << "will still choose Door #1.\n";
    for(int i = 0; i <= 1000; i++)
    {
        Stay(goat1, goat2, choice);
        temp = Stay(goat1, goat2, choice);
        staytotal = staytotal + temp;
    }
    stayratio = staytotal / 1000;
    cout << "Games Played: 1000   ";
    cout << "Games Won: " << staytotal << "   ";
    cout << "Win/Loss Ratio: " << stayratio << endl;

    // 1,000 Attempts of Switch Method
    cout << "\n-------------------------- The Switch Method -------------------------" << endl;
    cout << "The 'Switch' Method is when the contestant switches their choice after\n"
         << "the host reveals the first goat. For example, if the contestant picks\n"
         << "Door #1, and the host reveals the goat in Door #2, the contestant now\n"
         << "switches their choice to Door #3.\n";
    for (int j = 0; j <= 1000; j++)
    {
        Switch(goat1, goat2, choice);
        temp = Switch(goat1, goat2, choice);
        switchtotal = switchtotal + temp;
    }
    switchratio = switchtotal / 1000;
    cout << "Games Played: 1000   ";
    cout << "Games Won: " << switchtotal << "   ";
    cout << "Win/Loss Ratio: " << switchratio << endl;

    // 1,000 Attempts of Guess Method
    cout << "\n------------------------- The Guess Method -------------------------" << endl;
    cout << "The 'Guess' Method is when the contest goes with a 50/50 guess on\n"
         << "which door to open. For example, if the contestant picks Door #1,\n"
         << "and the host picks Door #2, the contestant would now take a 50/50\n"
         << "guess on which door to open, which can either be to stick with Door\n"
         << "#1 or to switch to Door #2.\n";
    for (int k = 0; k <= 1000; k++)
    {
        Guess(goat1, goat2, choice);
        temp = Guess(goat1, goat2, choice);
        guesstotal = guesstotal + temp;
    }
    guessratio = guesstotal / 1000;
    cout << "Games Played: 1000   ";
    cout << "Games Won: " << guesstotal << "   ";
    cout << "Win/Loss Ratio: " << guessratio << endl;
}

int DrawNum (int max)
{
    double x = RAND_MAX + 1.0;
    int y;

    y = static_cast<int> (1 + rand() * (max / x));
    return (y);
}

int Stay(int goat1, int goat2, int choice)
{
    int success = 1;
    int failure = 0;

    // Stay Method
    if (choice != goat2)
    {
        return success;
    }
    else if (choice == goat2)
    {
        return failure;
    }
}

int Switch(int goat1, int goat2, int choice)
{
    int success = 1;
    int failure = 0;

    // Switch Method
    if (choice == 1)
    {
        if (goat1 == 2)
        {
            choice = 3;
        }
        else if (goat1 == 3)
        {
            choice = 2;
        }
    }
    else if (choice == 2)
    {
        if (goat1 == 1)
        {
            choice = 3;
        }
        else if (goat1 == 3)
        {
            choice = 1;
        }
    }
    else if (choice == 3)
    {
        if (goat1 == 1)
        {
            choice = 2;
        }
        else if (goat1 == 2)
        {
            choice = 1;
        }
    }

    if (choice != goat2)
    {
        return success;
    }
    else if (choice == goat2)
    {
        return failure;
    }
}

int Guess(int goat1, int goat2, int choice)
{
    int success = 1;
    int failure = 0;
    int guess = 0;

    // Guess Method
    guess = DrawNum(2);
    if (guess == 1)
    {
        if (choice == 1)
        {
            if (goat1 == 2)
            {
                choice = 3;
            }
            else if (goat1 == 3)
            {
                choice = 2;
            }
        }
        else if (choice == 2)
        {
            if (goat1 == 1)
            {
                choice = 3;
            }
            else if (goat1 == 3)
            {
                choice = 1;
            }
        }
        else if (choice == 3)
        {
            if (goat1 == 1)
            {
                choice = 2;
            }
            else if (goat1 == 2)
            {
                choice = 1;
            }
        }

        if (choice != goat2)
        {
            return success;
        }
        else if (choice == goat2)
        {
            return failure;
        }
    }
    else if (guess == 2)
    {
        if (choice != goat2)
        {
            return success;
        }
        else if (choice == goat2)
        {
            return failure;
        }
    }
}


It's a chunky bit of code, but I've got no idea where to look.

Thanks in advance, guys.
do you understand reference parameters vs not?
At a guess, you think this changes something:
void foo( int x, int y)
{
x = 3;
y = 1415;
}

...
a = 100;
foo(a,b);
cout << a; //what does this print?
jonnin, I'm assuming its not going to be 100, also isn't foo(a,b) missing a value for b?

I think I'm missing something here...
I'll just complete jonnin's example so it compiles, and change the second parameter into a reference...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

void foo(int x, int& y)
{
    x = 3;
    y = 1415;
}

int main()
{
    using std::cout;
    int a = 100;
    int b = 200;
    foo(a, b);
    cout << a << ' ' << b << '\n'; //what does this print? 
}


https://www.geeksforgeeks.org/difference-between-call-by-value-and-call-by-reference/
Last edited on
I compiled it, and it printed 100 1415.

I still don't really understand why, and where that applies to my program.
I'm trying to understand, could you explain it?
I still don't really understand why, and where that applies to my program.
The intent (I assume) is for you to consider why this program prints 100 1415 and not 3 1415.

Once you understand this, you can apply that knowledge to your program to figure out why Stay, Switch, and Guess do not do what you intend.
Last edited on
To me the Monty Hall Problem seems obvious, but the related Bertrand's Box Paradox seems trickier.
https://en.wikipedia.org/wiki/Bertrand%27s_box_paradox
I still don't really understand why, and where that applies to my program.
I'm trying to understand, could you explain it?
On line 35/43 you make sure that goat1=goat2=choice which is always a failure (i.e. it stays 0). Neither Switch(...) nor Guess(...) will change choice in main() because you pass choice by value not reference. Thus the result will remain 0. goat1=goat2 will also remain the same throughout your program.

By the way: Why this double call on line 56/57, 73/74, 91/92?


https://www.tutorialspoint.com/cplusplus/cpp_function_call_by_reference.htm
https://www.tutorialspoint.com/cplusplus/cpp_function_call_by_value.htm
The most interesting of these conundrums that I've seen so far is the "Three Prisoners Problem." https://en.wikipedia.org/wiki/Three_Prisoners_problem

PROBLEM DESCRIPTION

Three prisoners in separate cells are sentenced to be executed next Tuesday.

The warden has chosen one at random to be pardoned,
but he is not allowed to tell them which one will be pardoned until the execution day.

Prisoner A pesters the warden into giving him the name of one of the other prisoners (B or C)
that will definitely be executed:
* if B will be pardoned, then the warden gives him C's name
* if C will be pardoned, then the warden gives him B's name
* if A will be pardoned, then the warden flips a coin to determine which of B's or C's name to give.

The warden tells A that B will definitely be executed.

Prisoner A feels that, given this information, his chances to be pardoned have risen from 1/3 to 1/2
since the pardon is now between him and C.

Prisoner A manages to get the information of B's definite execution to C.
Given this information, C feels that his chances to be pardoned have risen from 1/3 to 2/3.

Which of A or C, if either, is correct?

ANSWER: It turns out that C is correct.
coder777, THANK YOU. I was looking in the wrong place.
Topic archived. No new replies allowed.