Using JavaScript to solve a simple statistical challenge — should I change my choice?

Andrew Winnicki
Geek Culture
Published in
4 min readDec 5, 2022

--

Solving a common (apparently) statistical problem

I was walking from a pub with my team and had a heated debate about many different things, during which one guy asked a question, which apparently is a common statistical problem:

“There are 50 doors, and behind only one is a treasure. You can choose any door; I will remove the remaining 48 doors without the treasure. So you will be left with your door and one more, you know one of them must hide the treasure. You are allowed to change your choice now. Would you change it?”

Without much thinking, I said I wouldn’t. However, I’ve been informed that the right thing to do is to change the initial choice as it will give me a much higher chance of getting the treasure.

My thinking about it at the given moment

Whilst my initial choice is only a 2% chance, the second choice, whether I change it or not, is a 50% chance of getting it right. The logic I followed is similar to a coin toss — it is always a 50% chance to get “heads”, no matter what the previous toss was, but this example is not as similar as I thought. Every coin throw is an unrelated event. It’s not a continuation of the previous one, and I mistakenly assumed it is the same here.

I haven’t thought about the whole problem for too long and forgot about it soon… but the weekend came, and it came back to me like a boomerang.

Let’s talk about the boomerang

As I’m writing these words, I don’t know the answer yet, and I don’t want to search for it on the internet — I want to learn it myself.

Why do I now think my answer is wrong?

Except for the fact I’ve been told it’s wrong :)

Whilst it’s true that the first choice is 2% and the second 50%, this is only valid if I actually change my initial choice during the second opportunity. If I keep my initial choice, nothing will make it better than the mere 2%. The second change gives me the impression that these two are not connected events, and I genuinely have 50%, even if I stick with the initial door.

Note: I just read what I wrote so far, and I’m almost sure it will be 2% vs 50% and making the change is the right thing to do.

I turned to some JavaScript code to help me out.

Solving the problem using a bit of code

I wrote a simple function that will run 100,000 times. One run without changing the answer, and one with a different door choice change at the second opportunity. The first should have around a 2% success rate, and the second about 50%. Of course, since these runs generate random numbers, the score won’t be perfect, but with so many iterations, it will be damn close.

I kept the code simple, without any shortcuts, to ensure it was easy to follow. You can run it in a browser and adjust SHOULD_SWAP to true if you want to change the answer at the second opportunity. You can play with other variables like number of runs or amount of doors to see what results it will generate.

const RUNS = 100000;
const DOORS = 50;
const SHOULD_SWAP = false;

let allDoors = [];
let myChoice = null;
let otherDoors = null;

let result = [];
let accuracy = null;

function chooseDoors() {
// generate 50 doors, and make one treasure (true)
allDoors = Array(50).fill(false);
allDoors[Math.floor(Math.random() * DOORS)] = true;

// choose one door and eliminate remaining except 1
let selectedDoorsIndex = Math.floor(Math.random() * DOORS);
myChoice = allDoors[selectedDoorsIndex].toString();

// I don't have to chop the array, it's easy to figure out what the "otherDoors" will be
if (myChoice === 'false') {
otherDoors = 'true'
}
else {
otherDoors = 'false'
}

// swap the answer at second chance?
if (SHOULD_SWAP) {
myChoice = otherDoors;
}

result.push(myChoice);
}

// run X times
for (let i = 0; i < RUNS; i++) {
chooseDoors();
}

// calculate the final %
let trues = 0;
for (let item in result) {
if (result[item] === 'true') {
trues++;
}
}
accuracy = trues / RUNS * 100;

console.log(accuracy.toString() + '%');

Conclusion (not what I thought!)

I asked a few people about the answer, and the majority said 2% if I didn’t change my answer and 50% if I did. A few paragraphs earlier, I said that too!

It is exciting to learn that it is 2% and 98%!
IT MAKES SENSE… NOW! Of course, it will be 98! 😂

With a bit of curiosity, and a few lines of code, I’ve discovered something new about numbers. The most important lesson for the future is that things are not always what they seem, and no matter how confident I am, I might be simply wrong. It’s a reminder that changing our minds is not a bad idea either.

It reminds me of studies about changing our minds and how often people stay with their initial choice, believing it must be the best. The fear of regret is so intense that it affects our logical thinking. Let’s be honest; emotions control us more often than we want to believe. We are scared of what will happen if we change our minds at the second opportunity, but our door hides the treasure. The classic “I knew it! I knew I should stick to my door!” … and in 2% of cases that would be true :)

Challenge completed. It was fun :)

--

--

Andrew Winnicki
Geek Culture

Software Engineering Changemaker. Driving digital transformation and sharing experiences and thoughts from my journey. 20 years and counting…