Skip to content

Instantly share code, notes, and snippets.

@r3gor
Last active June 9, 2020 22:25
Show Gist options
  • Save r3gor/d69175871b138f3390363276aeb33ad1 to your computer and use it in GitHub Desktop.
Save r3gor/d69175871b138f3390363276aeb33ad1 to your computer and use it in GitHub Desktop.
Simulation to the Monty Hall Paradox
/*
Simulation to the Monty Hall Paradox
https://es.wikipedia.org/wiki/Problema_de_Monty_Hall
*/
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand (time(NULL));
unsigned int num_of_games = 100000;
unsigned int common_winners = 0;
unsigned int smart_winners = 0;
// monty hall - 100000 simulations
for (unsigned int i=0; i<num_of_games; i++ )
{
int door_prize = 1+rand()%3;
int common_decision = 1+rand()%3;
int revealed_door;
int smart_decision;
do
{
revealed_door = 1+rand()%3;
}while(revealed_door == door_prize || revealed_door == common_decision);
/*
the second decision must be different from
the first door chosen and also different from
the revealed door.
*/
do
{
smart_decision = 1+rand()%3;
} while (common_decision == smart_decision || revealed_door == smart_decision);
/*
the second decision must be different from the
first door chosen and also different from the
door revealed.
*/
unsigned int random_participant_type = rand() % 2 + 1;
switch (random_participant_type)
{
case 1: // common participant
if (common_decision == door_prize) common_winners++;
break;
case 2: // smart participant
if (smart_decision == door_prize) smart_winners++;
break;
}
} // end simulations, results:
cout<<endl<<".:SMARTS:."<<endl;
cout<< "winners: "<<smart_winners<<endl;
cout<< "percentage of success: "<<smart_winners*100/num_of_games<<" %"<<endl;
cout<<endl;
cout<<endl<<".:COMMONS:."<<endl;
cout<< "winners: "<<common_winners<<endl;
cout<< "percentage of success: "<<common_winners*100/num_of_games<<" %"<<endl;
cout<<endl;
system("pause");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment