C++ - The Monty Hall Game Show
Auteur: Addow - 03 november 2006 - 20:26 - Gekeurd door: Joel - Hits: 2284 - Aantal punten: (0 stemmen)
[ The Monty Hall Game Show ]
Simuleert een deelname aan de Monty Hall Game Show, waarbij je als
finalist voor 3 deuren staat. Achter 1 van deze 3 deuren bevindt zich een luxe-wagen.
Spelverkoop:
1. Je kiest een deur
2. De quizkwater opent 1 van de andere deuren
3. Je mag je keuze wijzigen en/of behouden
4. Je weet of je de wagen wint of niet
De simulatie doorloopt deze situatie zo'n 10.000x en berekent vervolgens de percentages waarbij je gewonnen hebt en:
1. NIET-gewisseld hebt
2. wél gewisseld hebt.
Het resultaat... verrassend??
|
Code: |
<?
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
// header() - Print out some header information containing de name of the program
void header() {
cout << "The Monty Hall Game Show" << endl;
cout << "========================" << endl;
}
// putCar() - Put the car behind 1 of the 3 doors randomly
int putCar() {
return rand()%3+1;
}
// pickDoor() - Choose 1 of 3 doors where YOU think the car is behind
int pickDoor() {
return rand()%3+1;
}
// openDoor() - Open 1 of the 2 other doors randomly
int openDoor(int yourChoice) {
int door = yourChoice;
while(door==yourChoice)
door = rand()%3+1;
return door;
}
// keepChoice() - Make a doorswitch or not, if 1, pick last door, if 0, keep yourChoice
int keepChoice(int yourChoice, int openedDoor) {
if(rand()%2)
return 6-yourChoice-openedDoor;
else
return yourChoice;
}
// main() - Main program
int main() {
srand((unsigned)time(NULL));
int percWinNC = 0, percWinCH = 0, percLost = 0;
// print header
header();
for(int i=0; i<10000; i++) {
int nmbr = rand()%3+1;
int car = putCar();
int choice = pickDoor();
int door = openDoor(choice);
int final = keepChoice(choice, door);
if(choice==final && choice==car) percWinNC++;
else if(choice!=final && final==car) percWinCH++;
else percLost++;
}
cout << " .Probability of winning, no change: " << percWinNC/100.0 << "%" << endl;
cout << " .Probability of winning, after change: " << percWinCH/100.0 << "%" << endl;
cout << " .Probability of losing: " << percLost/100.0 << "%" << endl;
}
<? #include <iostream> #include <stdlib.h> #include <time.h> using namespace std; // header() - Print out some header information containing de name of the program cout << "The Monty Hall Game Show" << endl; cout << "========================" << endl; } // putCar() - Put the car behind 1 of the 3 doors randomly int putCar() { } // pickDoor() - Choose 1 of 3 doors where YOU think the car is behind int pickDoor() { } // openDoor() - Open 1 of the 2 other doors randomly int openDoor(int yourChoice) { int door = yourChoice; while(door==yourChoice) return door; } // keepChoice() - Make a doorswitch or not, if 1, pick last door, if 0, keep yourChoice int keepChoice(int yourChoice, int openedDoor) { return 6-yourChoice-openedDoor; else return yourChoice; } // main() - Main program int main() { int percWinNC = 0, percWinCH = 0, percLost = 0; // print header for(int i=0; i<10000; i++) { int car = putCar(); int choice = pickDoor(); int door = openDoor(choice); int final = keepChoice(choice, door); if(choice==final && choice==car) percWinNC++; else if(choice!=final && final==car) percWinCH++; else percLost++; } cout << " .Probability of winning, no change: " << percWinNC/100.0 << "%" << endl; cout << " .Probability of winning, after change: " << percWinCH/100.0 << "%" << endl; cout << " .Probability of losing: " << percLost/100.0 << "%" << endl; }
Download code (.txt)
|
|
|
Stemmen |
Niet ingelogd. |
|