mardi 27 octobre 2015

Monty Hall Simulator

This code compiles and runs, but I don't get the correct percentage for the Monty Hall problem. The answer should be closer to 66% for switching. I cannot find where the problem is. The percentage for staying with the same door is right, but the switching, which includes the switchChoice and the revealedDoor function is giving consistent but incorrect results.

#include <iostream>
#include <vector>
#include <iomanip>
#include <time.h> 

using namespace std;
int winner();
int userChoice();
int switchChoice(int, int);
bool decision( int, int);
vector <int> simulation();
void analytics();
int revealedDoor();

int main(int argc, char *argv[]) {
    time_t seconds;
    time(&seconds);
    srand((unsigned int)seconds);
analytics();    
}
int winner(){
    return  (rand() / ( RAND_MAX / 3 ) + 1);
}
int userChoice(){
    return (rand() / ( RAND_MAX / 3 ) + 1);
}
int revealedDoor(int winnerX){  
    if (winnerX == 1){
            return  (2 + rand() % 2);
        }
        else if (winnerX == 2){
            if( ( 3 + rand() % 2) == 4){
                return  1;
            }else{
                return  3;
            }
        }
        else if (winnerX == 3){
            return  (1 + rand() % 2);
        }
    else return winnerX;
}
int switchChoice(int uChoice, int revealedD){
    int newChoice;
    do{
        newChoice =  1 + rand() % 3;
    }while ( uChoice == newChoice || newChoice == revealedD);

    return newChoice;
}
bool decision(int uChoice, int winnerX){
        if(uChoice == winnerX){
            return true;
        }else{
            return false;
        }

}
vector<int> simulation (){
    long n, winnerX, uChoice = 0;
    vector<int> choiceCtr;
    choiceCtr.push_back(0);
    choiceCtr.push_back(0);

    cout << "How many times would you like to run the simulation? ";
    cin >> n;
    choiceCtr.push_back(n);

    for(int i = 0; i < n; i++){
        winnerX = winner();
            if(decision(switchChoice(userChoice(), revealedDoor(winnerX)), winnerX)){
                choiceCtr[0]++;             
            }
    }
    for(int i = 0; i < n; i++){
    if(decision(userChoice(), winner())){
        choiceCtr[1]++;             
    }
    }
    return choiceCtr;
}

void analytics(){
    vector<int> choices;

    choices = simulation();

    double percent = choices[0] / static_cast<double>(choices[2]);
    double percent2 = choices[1] / static_cast<double>(choices[2]);

    cout << choices[0] << endl;
    cout << choices[1] << endl;

    cout << fixed << setprecision(2)<< "Switching: " << endl << "-----------------" << endl << "You won " << choices[0] << " out of " << choices[2] << " Which is " << percent * 100  << " percent." << endl << endl;
    cout << fixed << setprecision(2)<< "Staying: " << endl << "-----------------" << endl << "You won " << choices[1] << " out of " << choices[2] << " Which is " << percent2 * 100  << " percent." << endl << endl;
}




Aucun commentaire:

Enregistrer un commentaire