login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > Overige > C en C++ > C++ - The Monty Hall Game Show

C++ - The Monty Hall Game Show

Auteur: Addow - 03 november 2006 - 20:26 - Gekeurd door: Joel - Hits: 2194 - 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:
  1. <?
  2. #include <iostream>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. using namespace std;
  6.  
  7. // header() - Print out some header information containing de name of the program
  8. void header() {
  9. cout << "The Monty Hall Game Show" << endl;
  10. cout << "========================" << endl;
  11. }
  12.  
  13. // putCar() - Put the car behind 1 of the 3 doors randomly
  14. int putCar() {
  15. return rand()%3+1;
  16. }
  17.  
  18. // pickDoor() - Choose 1 of 3 doors where YOU think the car is behind
  19. int pickDoor() {
  20. return rand()%3+1;
  21. }
  22.  
  23. // openDoor() - Open 1 of the 2 other doors randomly
  24. int openDoor(int yourChoice) {
  25. int door = yourChoice;
  26.  
  27. while(door==yourChoice)
  28. door = rand()%3+1;
  29.  
  30. return door;
  31. }
  32.  
  33. // keepChoice() - Make a doorswitch or not, if 1, pick last door, if 0, keep yourChoice
  34. int keepChoice(int yourChoice, int openedDoor) {
  35. if(rand()%2)
  36. return 6-yourChoice-openedDoor;
  37. else
  38. return yourChoice;
  39. }
  40.  
  41. // main() - Main program
  42. int main() {
  43. srand((unsigned)time(NULL));
  44. int percWinNC = 0, percWinCH = 0, percLost = 0;
  45.  
  46. // print header
  47. header();
  48.  
  49. for(int i=0; i<10000; i++) {
  50. int nmbr = rand()%3+1;
  51. int car = putCar();
  52. int choice = pickDoor();
  53. int door = openDoor(choice);
  54. int final = keepChoice(choice, door);
  55.  
  56. if(choice==final && choice==car) percWinNC++;
  57. else if(choice!=final && final==car) percWinCH++;
  58. else percLost++;
  59. }
  60.  
  61. cout << " .Probability of winning, no change: " << percWinNC/100.0 << "%" << endl;
  62. cout << " .Probability of winning, after change: " << percWinCH/100.0 << "%" << endl;
  63. cout << " .Probability of losing: " << percLost/100.0 << "%" << endl;
  64. }
Download code! Download code (.txt)

 Bekijk een voorbeeld van dit script!
 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Lees de reacties (8)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.022s