Created
March 2, 2016 16:33
-
-
Save AtharvaVaidya/53bffce3458dddb4d456 to your computer and use it in GitHub Desktop.
This file contains 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
// Lab #8 | |
// Created by Atharva Vaidya on 02/29/16. | |
// Copyright © 2015 Atharva Vaidya. All rights reserved. | |
#include <iostream> | |
#include <iomanip> | |
#include <cstdlib> | |
#include <time.h> | |
#include <cmath> | |
using namespace std; | |
int randInRange(int n) | |
{ | |
double x = ( double(n) * (double)rand() / ((double) RAND_MAX + 1)); | |
return int(x); | |
} | |
void setupDoors(char &door1, char &door2, char &door3) | |
{ | |
randInRange(1000); | |
int doorForCar = randInRange(3); | |
switch (doorForCar) { | |
case 0: | |
door1 = 'C'; | |
door2 = 'G'; | |
door3 = 'G'; | |
break; | |
case 1: | |
door1 = 'G'; | |
door2 = 'C'; | |
door3 = 'G'; | |
break; | |
case 2: | |
door1 = 'G'; | |
door2 = 'G'; | |
door3 = 'C'; | |
break; | |
default: | |
break; | |
} | |
} | |
void pickDoorChoices(char door1, char door2, char door3, int &doorPlayer, int &doorMonty) | |
{ | |
char doors[] = {door1, door2, door3}; | |
doorPlayer = randInRange(3) + 1; | |
do | |
{ | |
doorMonty = randInRange(3) + 1; | |
} | |
while (doorMonty == doorPlayer || doors[doorMonty] == 'C'); | |
} | |
int main(void) | |
{ | |
int seed = static_cast<int>(time(0)); | |
srand(seed); | |
char d1, d2, d3; | |
char doors[] = {d1, d2, d3}; | |
int doorPlayer = 0, doorMonty, switchToWin = 0, dontSwitchToWin = 0; | |
for (int i = 0; i < 100; i++) | |
{ | |
for (int i = 0; i < 100000; i++) | |
{ | |
setupDoors(d1, d2, d3); | |
pickDoorChoices(d1, d2, d3, doorPlayer, doorMonty); | |
(doors[doorPlayer] == 'C') ? dontSwitchToWin++ : switchToWin++; | |
} | |
cout << setprecision(3) << fixed; | |
cout << double(dontSwitchToWin) * 100 / double(dontSwitchToWin + switchToWin) << "% | " << double(switchToWin) * 100 / double(dontSwitchToWin + switchToWin) << "%" << endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment