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
*/
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';
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:
//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);
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.
1 2 3 4
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.
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...
#include <iostream>
#include <cmath>
#include <iomanip>
usingnamespace 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 . . .