### Monty Hall Conundrum

i am trying to write a code to simulate the Monty Hall Probability problem... I'm having issues with the tally in lines 53-63. for some reason its not catching all the instances. any advice? I'm pretty much a total beginner.. so please keep that in mind.

/*
* File: montyHall.cpp
*
* Created on February 24, 2012, 12:08 PM
*
* Monty Hall simulation
*/

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

/*
*
*/

int main()
{

//seed random number generator
int srand ( time(NULL) );

//declare all needed variables
double prize_door = 0;
double guess_door = 0;
double reveal_door = 0;
static double stay_results;
static double switch_results;
static int i = 0;
static int stay_door;
static int switch_door;

do
{
//generate a random number and set it to prize_door
prize_door = rand() % 3;
//cout << "prize door = " << prize_door << endl;

//generate a random number and set it to guess_door
guess_door = rand() %3;
//cout << "guess door = " << guess_door << endl;

do
{
reveal_door = rand() % 3;
//cout << "reveal door = " << reveal_door << endl;
}while (reveal_door == guess_door || reveal_door == prize_door);

//tally counters
if ( switch_door == prize_door )
{
switch_door++;
cout << switch_door << endl;
}

if (stay_door == prize_door)
{
stay_door++;
cout << stay_door << endl;
}

i++;
}while (i < 14099);

stay_results = stay_door % 100;
switch_results = switch_door % 100;

cout << "the percentage of wins when staying is:\t\t"
<< stay_results << '\n';
cout << "the percentage of wins when switching is:\t"
<< switch_results << '\n';

return 0;
}
 ``12`` ``````stay_results = stay_door % 100; switch_results = switch_door % 100;``````

That's wrong. I think you meant:

 ``12`` ``````stay_results = stay_door *100 / ( stay_door + switch_door); switch_results = switch_door *100 / ( stay_door + switch_door);``````
Could you use code tags next time?

But here's the thing:
In your tally counters you use `if ( switch_door == prize_door )`. However, what is `switch_door`? Check the stuff before-hand. You never set this (edit: the `static` keyword initializes at 0... but you use it in two completely different ways. stay_door could be 10000 but it would obviously never equal prize_door in that case, it would actually never exceed 2 because of this logic).

I'm guessing that you mean to do something like this:
 ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````//Initialize stay_door and switch_door to 0; do { //generate a random number and set it to prize_door prize_door = rand() % 3; //generate a random number and set it to guess_door guess_door = rand() %3; //Guess a door if ( guess_door == prize_door ) { stay_door++; cout << switch_door << endl; } //Reveal a door do { reveal_door = rand() % 3; //cout << "reveal door = " << reveal_door << endl; }while (reveal_door != guess_door && reveal_door != prize_door); //Switch to the other unrevealed door do { new_guess_door = rand() % 3; }while (new_guess_door != reveal_door && new_guess_door != guess_door ); //Did you win? if ( new_guess_door == prize_door ) { switch_door++; cout << switch_door << endl; } i++; }while (i < 14099);``````
Last edited on
i am trying to get a simple percentage, could i not just divide

stay_results / 14100
switch_results / 14100

the goal is to have each loop end up in either of the two cases...
The reason you can't divide is called integer division. This means that any decimal points aren't preserved when you divide two integers.

A solution would be to turn these values into floats to ensure that the decimal is preserved.
 ``1234`` ``````cout << "the percentage of wins when staying is:\t\t" << (float)stay_door / 100.f << '\n'; cout << "the percentage of wins when switching is:\t" << (float)switch_door / 100.f << '\n';``````

OH an using a modulus (%) just gets the remainder so it doesn't help if you are exceeding 100 a few times in this situation.
Last edited on
thanks you guys! both suggestions helped immensely...

any thoughts on the outcome? its supposed to show that you are more likely to win if you switch after one door is revealed. but my numbers are backwards...
This works for me:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859`` ``````#include #include #include using namespace std; int main() { //seed random number generator int srand ( time(NULL) ); //declare all needed variables int prize_door, guess_door, reveal_door, new_guess_door, i = 0, stay_door = 0, switch_door = 0; //Initialize stay_door and switch_door to 0; do { //generate a random number and set it to prize_door prize_door = rand() % 3; //generate a random number and set it to guess_door guess_door = rand() %3; //Guess a door if ( guess_door == prize_door ) { stay_door++; cout << stay_door << endl; } //Reveal a door do { reveal_door = rand() % 3; }while (reveal_door != guess_door && reveal_door != prize_door); //Switch to the other unrevealed door do { new_guess_door = rand() % 3; }while (new_guess_door != reveal_door && new_guess_door != guess_door ); //Did you win? if ( new_guess_door == prize_door ) { switch_door++; cout << switch_door << endl; } i++; }while (i < 14099); cout << "the percentage of wins when staying is:\t\t" << (float)stay_door/100.f << '\n'; cout << "the percentage of wins when switching is:\t" << (float)switch_door/100.f << '\n'; return 0; }`````` ```... 7020 7021 4683 7022 4684 7023 4685 7024 4686 7025 7026 4687 7027 4688 7028 4689 7029 4690 7030 4691 7031 4692 7032 4693 7033 4694 7034 7035 4695 7036 7037 4696 7038 4697 7039 7040 7041 7042 4698 7043 4699 7044 4700 7045 the percentage of wins when staying is: 47 the percentage of wins when switching is: 70.45 Press any key to continue . . .```
Last edited on
scratch that... it seems to be working fine now.. thanks for your help!
Topic archived. No new replies allowed.