Skip to content

Instantly share code, notes, and snippets.

@nodech
Created September 4, 2017 22:53
Show Gist options
  • Save nodech/759246ee876a614f88fb522b8acdcac1 to your computer and use it in GitHub Desktop.
Save nodech/759246ee876a614f88fb522b8acdcac1 to your computer and use it in GitHub Desktop.
Monty Hall Problem (Paradox)
const percent = (a, b) => {
return (a / b * 100).toFixed(2);
}
const getRandomIndex = (n) => {
return Math.floor(Math.random() * n);
};
const getRandomNumbers = (n) => {
const arr = new Array(n);
const index = getRandomIndex(n);
arr[index] = 1;
return arr;
};
const chooseRandom = (skipIndexes, n) => {
const rand = getRandomIndex(n - skipIndexes.length);
for (let i = 0, c = 0; i < n; i++) {
if (skipIndexes.indexOf(i) !== -1) continue;
if (c === rand) return i;
c++;
}
};
const revealRandChangeRand = (question, answer, N) => {
const rightAnswer = question.indexOf(1);
const reveal = chooseRandom([rightAnswer, answer], N);
return chooseRandom([answer, reveal], N);
};
const revealRandChangeNext = (question, answer, N) => {
const rightAnswer = question.indexOf(1);
const reveal = chooseRandom([rightAnswer, answer], N);
return question.findIndex((_, i) => i !== answer && i !== reveal);
};
const revealNextChangeNext = (question, answer, N) => {
const rightAnswer = question.indexOf(1);
const reveal = question.findIndex((_, i) => i !== rightAnswer && i !== answer);
return question.findIndex((_, i) => i !== answer && i !== reveal);
};
const revealNextChangeRand = (question, answer, N) => {
const rightAnswer = question.indexOf(1);
const reveal = question.findIndex((_, i) => i !== rightAnswer && i !== answer);
return chooseRandom([answer, reveal], N);
};
const TESTS = 1000000;
const N = 3;
let statsRandRand = 0;
let statsRandNext = 0;
let statsNextNext = 0;
let statsNextRand = 0;
for (let i = 0; i < TESTS; i++) {
const question = getRandomNumbers(N);
const answer = getRandomIndex(N);
const rightAnswer = question.indexOf(1);
const changeRandRand = revealRandChangeRand(question, answer, N);
const changeRandNext = revealRandChangeNext(question, answer, N);
const changeNextNext = revealNextChangeNext(question, answer, N);
const changeNextRand = revealNextChangeRand(question, answer, N);
if (changeRandRand === rightAnswer) statsRandRand++;
if (changeRandNext === rightAnswer) statsRandNext++;
if (changeNextNext === rightAnswer) statsNextNext++;
if (changeNextRand === rightAnswer) statsNextRand++;
}
const original = percent(1, N);
const printResult = (name, win) => {
const loose = TESTS - win;
const winP = percent(win, TESTS);
const looseP = percent(loose, TESTS);
console.log(`${name}:`);
console.log(`Won: ${win} (${winP} %), Lost: ${loose} (${looseP} %)`);
console.log('-----');
console.log('');
};
console.log('Expected %:', original)
console.log('-----');
console.log('');
printResult('RandRand', statsRandRand);
printResult('RandNext', statsRandNext);
printResult('NextNext', statsNextNext);
printResult('NextRand', statsNextNext);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment