int main(void) { const int numDoors = 3; // results[0] = wins, results[1] = losses int results[2] = {0}; char playAgain = 'Y'; // Set-up RNG init_random_number_generator(); // Intro to simulator printf("Welcome to the Monty Hall Problem Simulator!\n\n"); printf("Monty Hall Problem:\nSuppose you're on a game show, \ and you're given the choice of three doors: Behind one door \ is a car; behind the others, goats. You pick a door, say No. \ 1, and the host, who knows what's behind the doors, opens \ another door, say No. 3, which has a goat. He then says to \ you, \"Do you want to pick door No. 2?\" Is it to your \ advantage to switch your choice?\n"); while (playAgain == 'Y' || playAgain == 'y') { // How many repeated experiments? int numPlays = 1; printf("\nHow many plays: "); scanf("%d", &numPlays); // Launch experiments for (int j = 0; j < numPlays; j++) { // Always start with door 1 as choice int choice = 0; // Set-up doors (2 x numDoors array) // first row holds winning/losing door info // second row holds info about whether door is open bool **doors = setup_doors(numDoors); // Open first false door that we didn't pick int losingDoor = 0; while (losingDoor == choice || doors[0][losingDoor]) { ++losingDoor; } doors[1][losingDoor] = true; // Switch choice to other door // or the first door we didnt pick and is not opened // (for experiments with more than 3 doors) int otherDoor = 0; while (otherDoor == losingDoor || otherDoor == choice) { ++otherDoor; } choice = otherDoor; // record results doors[1][choice] = true; if (doors[0][choice] == false) { // loss results[1]++; } else { // win results[0]++; } // clean-up memory destroy_doors(doors); } // print results printf("Times win car after switching choice: %d\n", results[0]); printf("Times win goat after switching choice: %d\n\n", results[1]); printf("Play Again? (Y/N): "); scanf(" %c", &playAgain); }
__attribute__((no_instrument_function)) void os_init(void) { setup_doors(); setup_rooms(); not_main(); }