Hacker News new | ask | show | jobs
by semiotagonal 2427 days ago
RESCINDED: Error in my simulation! Apologies.
1 comments

You're accidentally including the cases where montysPick == myPick. You have to discard those cases.

Monte has to show you what's behind a different door than the one you initially picked. The game doesn't make sense otherwise.

Yes you are correct.

Corrected simulation gives 50/50 when Monty picks at random among 3 doors.

switchWins=33044 switchLoses=33581

#include <stdio.h> #include <stdlib.h> #include <time.h> #include <string.h>

#define NUM_RUNS 100000 #define NUM_DOORS 3 int main(int argc, const char* argv[]) { srand(time(NULL)); char doors[NUM_DOORS]; int switchLoses = 0; int switchWins = 0; for(int i = 0; i < NUM_RUNS; i++) { memset(&doors, 0, sizeof(doors)); doors[rand() % NUM_DOORS] = 1; int myPick = rand() % NUM_DOORS; int montysPick = rand() % (NUM_DOORS - 1); if(montysPick >= myPick) montysPick++; if(doors[montysPick]) continue; if(doors[myPick]) switchLoses++; else switchWins++; } printf("switchWins=%d switchLoses=%d\n", switchWins, switchLoses); return 0; }

EDITED TO ADD:

Just for further info:

3 doors: switchWins=33111 switchLoses=33470

4 doors: switchWins=50182 switchLoses=24698

8 doors: switchWins=74876 switchLoses=12687

100000 doors: switchWins=99998 switchLoses=2

Hah, I wrote one as well to prove it to myself in the chrome dev tools console

  const pickCar = (switchChoice) => {
    const doors = [0, 0, 0];
    doors[(Math.random() * doors.length - 1) | 0] = 1
 let pick = (Math.random() * doors.length - 1) | 0
 let goat
 for(let i = 0; i < doors.length; i++) { if (doors[i] == 0 && i != pick) goat = i  }
 let result = pick
 if (switchChoice) for(let i = 0; i < doors.length; i++) { if (i != pick && i != goat) result = i }
 return result 
  }
  let switchWins = 0;
  let switchLosses = 0;
  for(let i = 0; i < 100000; i++) {
   pickCar(true) == 1 ? switchWins++ : switchLosses++
  }
  console.log('wins:', switchWins, ', losses: ',switchLosses)