Skip to content

Instantly share code, notes, and snippets.

@diversen
Created January 25, 2023 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diversen/1fd31612efce74ce62bf1420e4d7ecf4 to your computer and use it in GitHub Desktop.
Save diversen/1fd31612efce74ce62bf1420e4d7ecf4 to your computer and use it in GitHub Desktop.
// Mathematics. The problem of Hannibal's organs
// https://twitter.com/pickover/status/1618030222246580224
let JAR = []
let BRAIN = 0
let KIDNEY = 1
// Random add a brain or a kidney
function init() {
JAR = []
Math.random() > 0.5 ? JAR.push(BRAIN) : JAR.push(KIDNEY)
}
// Add a brain
function addBrain() {
JAR.push(BRAIN)
}
// Draw from JAR, remove element and return element
function draw() {
let index = Math.floor(Math.random()*JAR.length)
let draw = JAR[index];
JAR.splice(index, 1)
return draw
}
let BRAINS_DRAWN = 0
let KIDNEYS_DRAWN = 0
let NUM_TESTS = 10000
let USABLE_TESTS = 0
for(let i = 0; i < NUM_TESTS; i++) {
init()
addBrain()
// Hanibal draws
// If it is not a brain then the expiriment will not be used.
let res = draw()
if (res !== BRAIN) {
continue;
}
USABLE_TESTS +=1
res = draw()
if (res === BRAIN) {
BRAINS_DRAWN += 1
}
if (res === KIDNEY) {
KIDNEYS_DRAWN += 1
}
}
console.log(`Chance of a usable test `, USABLE_TESTS / NUM_TESTS)
console.log(`Chance of drawing a BRAIN out of ${USABLE_TESTS}`, BRAINS_DRAWN / USABLE_TESTS)
// -> about 2/3 ~ 0.66....
console.log(`Chance of drawing a KIDNEY out of ${USABLE_TESTS}`, KIDNEYS_DRAWN / USABLE_TESTS)
// -> about 1/3 ~ 0.33....
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment