Skip to content

Instantly share code, notes, and snippets.

@Krinkle
Created August 10, 2025 21:38
Show Gist options
  • Save Krinkle/342cef278a384948c5ac7430864a3d6e to your computer and use it in GitHub Desktop.
Save Krinkle/342cef278a384948c5ac7430864a3d6e to your computer and use it in GitHub Desktop.
/*!
* Usage:
* $ node montehall.js
* $ DEBUG=1 node montehall.js
*
* changeYourMind=true
*
* $ node montehall.js | grep won | wc -l
* ~660
*
* changeYourMind=false
*
* $ node montehall.js | grep won | wc -l
* ~330
*/
const util = require('util');
const DEBUG = process.env.DEBUG;
function getRandomInt(max) {
return 1 + Math.floor(Math.random() * max);
}
function getRandomValue(list) {
return list[Math.floor(Math.random() * list.length)];
}
function debug(msg) {
if (DEBUG) {
console.log(util.styleText('grey', msg));
};
}
/**
* @param {bool} changeYourMind
*/
function playMonteHall(changeYourMind) {
var doors = [ '#1', '#2' ,'#3' ];
var secret = getRandomValue(doors);
var choice = getRandomValue(doors);
console.log('You choose door ' + choice);
debug('... prize is behind ' + secret);
var allGoats = doors.filter(value => value !== secret);
debug('... all goats: ' + allGoats);
var remainingGoats = allGoats.filter(value => value !== choice);
debug('... remaining goats: ' + remainingGoats);
var shownGoat = getRandomValue(remainingGoats);
console.log('Goat behind door ' + shownGoat);
var remainingDoor = doors.filter(value => value !== choice && value !== shownGoat)[0];
console.log('Do you want to change to door ' + remainingDoor + '?');
if (!changeYourMind) {
console.log('[STICK]',
secret === choice
? util.styleText('green', 'You won!')
: util.styleText('red', `You lost, it was door ${secret}.`)
);
} else {
var newChoice = remainingDoor;
console.log('[CHANGE]',
secret === newChoice
? util.styleText('green', 'You won!')
: util.styleText('red', `You lost, it was door ${secret}.`)
);
}
}
var i = 1000;
while (i--) {
playMonteHall(true);
}
/*
# START
3 starting layouts
[ πŸ† ] [ 🐐 ] [ 🐐 ]
[ 🐐 ] [ πŸ† ] [ 🐐 ]
[ 🐐 ] [ 🐐 ] [ πŸ† ]
3 starting choices
[ πŸ”Ή] [ ] [ ]
[ ] [ πŸ”Ή ] [ ]
[ ] [ ] [ πŸ”Ή]
9 starting scenarios
[ πŸ†πŸ”Ή] [ 🐐 ] [ 🐐 ]
[ πŸ† ] [ πŸπŸ”Ή] [ 🐐 ]
[ πŸ† ] [ 🐐 ] [ πŸπŸ”Ή]
[ πŸπŸ”Ή] [ πŸ† ] [ 🐐 ]
[ 🐐 ] [ πŸ†πŸ”Ή] [ 🐐 ]
[ 🐐 ] [ πŸ† ] [ πŸπŸ”Ή]
[ πŸπŸ”Ή] [ 🐐 ] [ πŸ† ]
[ 🐐 ] [ πŸπŸ”Ή] [ πŸ† ]
[ 🐐 ] [ 🐐 ] [ πŸ†πŸ”Ή]
Chance of win:
3 in 9
= 1 in 3
= 33%
---------
# THE GOAT
9 starting scenarios show you the one or two remaining goats (not prize, not choice)
[ πŸ†πŸ”Ή] [ 🐐 ] [ 🐐 ]
[ πŸ†πŸ”Ή] [ 🐐❌] [ 🐐2️⃣]
[ πŸ†πŸ”Ή] [ 🐐2️⃣] [ 🐐❌]
[ πŸ† ] [ πŸπŸ”Ή] [ 🐐 ]
[ πŸ†2️⃣] [ πŸπŸ”Ή] [ 🐐❌]
[ πŸ†2️⃣] [ πŸπŸ”Ή] [ 🐐❌]
[ πŸ† ] [ 🐐 ] [ πŸπŸ”Ή]
[ πŸ†2️⃣] [ 🐐❌] [ πŸπŸ”Ή]
[ πŸ†2️⃣] [ 🐐❌] [ πŸπŸ”Ή]
[ πŸπŸ”Ή] [ πŸ† ] [ 🐐 ]
[ πŸπŸ”Ή] [ πŸ†2️⃣] [ 🐐❌]
[ πŸπŸ”Ή] [ πŸ†2️⃣] [ 🐐❌]
[ 🐐 ] [ πŸ†πŸ”Ή] [ 🐐 ]
[ 🐐2️⃣] [ πŸ†πŸ”Ή] [ 🐐❌]
[ 🐐❌] [ πŸ†πŸ”Ή] [ 🐐2️⃣]
[ 🐐 ] [ πŸ† ] [ πŸπŸ”Ή]
[ 🐐❌] [ πŸ†2️⃣] [ πŸπŸ”Ή]
[ 🐐❌] [ πŸ†2️⃣] [ πŸπŸ”Ή]
[ πŸπŸ”Ή] [ 🐐 ] [ πŸ† ]
[ πŸπŸ”Ή] [ 🐐❌] [ πŸ†2️⃣]
[ πŸπŸ”Ή] [ 🐐❌] [ πŸ†2️⃣]
[ 🐐 ] [ πŸπŸ”Ή] [ πŸ† ]
[ 🐐❌] [ πŸπŸ”Ή] [ πŸ†2️⃣]
[ 🐐❌] [ πŸπŸ”Ή] [ πŸ†2️⃣]
[ 🐐 ] [ 🐐 ] [ πŸ†πŸ”Ή]
[ 🐐❌] [ 🐐2️⃣] [ πŸ†πŸ”Ή]
[ 🐐2️⃣] [ 🐐❌] [ πŸ†πŸ”Ή]
18 choices:
* 6 in 18: on the prize, change to goat
* 12 in 18: on the goat, change to prize
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment