Last active
December 29, 2015 14:49
-
-
Save sillykelvin/7686899 to your computer and use it in GitHub Desktop.
Solve Monty Hall problem (AKA Three Door problem)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <random> | |
typedef std::mt19937 EngineType; | |
typedef std::uniform_int_distribution<std::mt19937::result_type> GeneratorType; | |
bool GetIt(EngineType& engine, const GeneratorType& generator3, bool change) { | |
int correct_door = generator3(engine); | |
int first_choice = generator3(engine); | |
if(first_choice == correct_door) { | |
if(change) return false; | |
else return true; | |
} else { | |
if(change) return true; | |
else return false; | |
} | |
} | |
int main() { | |
EngineType eng; | |
eng.seed(std::random_device()()); | |
GeneratorType dist3(1, 3); | |
int change_sum = 0, unchange_sum = 0; | |
long loop = 100000000; | |
for(long i = 0; i < loop; ++i) { | |
if(GetIt(eng, dist3, true)) | |
++change_sum; | |
if(GetIt(eng, dist3, false)) | |
++unchange_sum; | |
} | |
std::cout << "Testing times: " << loop << std::endl; | |
std::cout << "Always change choice, hit: " << change_sum | |
<< ", percentage: " << static_cast<double>(change_sum) / loop | |
<< std::endl; | |
std::cout << "Always unchange choice, hit: " << unchange_sum | |
<< ", percentage: " << static_cast<double>(unchange_sum) / loop | |
<< std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment