Skip to content

Instantly share code, notes, and snippets.

@acagastya
Created January 6, 2020 08:28
Show Gist options
  • Save acagastya/0c1bfeb3aaadec4d0f9ab1a39e6dcaa3 to your computer and use it in GitHub Desktop.
Save acagastya/0c1bfeb3aaadec4d0f9ab1a39e6dcaa3 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
short int reveal(short int firstDoor, short int moneyDoor) {
short int arr[3] = {0, 1, 2};
if (firstDoor == moneyDoor) {
short int newArr[2];
short int j = 0;
for (short int i = 0; i < 3; i++) {
if (i == moneyDoor) continue;
newArr[j++] = i;
}
return newArr[rand() % 2];
} else
return 3 - firstDoor - moneyDoor;
}
int main() {
srand(time(NULL));
const unsigned int tries = 2500000000;
static unsigned int stayWins = 0;
static unsigned int swapWins = 0;
for (unsigned int i = 0; i < tries; i++) {
short int moneyDoor = (short int)(rand() % 3);
short int firstChoice = (short int)(rand() % 3);
short int revealedDoor = reveal(firstChoice, moneyDoor);
if (firstChoice == moneyDoor)
stayWins++;
else {
short int swappedDoor = 3 - firstChoice - revealedDoor;
if (swappedDoor == moneyDoor) swapWins++;
}
}
printf("Swapped wins: %u\nSwap win percent: %f%%\n", swapWins, ((1.0 * swapWins)/tries)*100);
printf("Stayed wins: %u\nStay win percent: %f%%\n", stayWins, ((1.0 * stayWins)/tries)*100);
printf("Ratio: %f\n", ((swapWins * 1.0)/stayWins));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment