JSFiddle - React, Tailwind, and code Playground
CSS
from random import shuffle
from copy import deepcopy
boxes = [ [100,100], [100,1]]
other_note_was_1=0
other_note_was_100=0
# Perform the experiment a million times
for _ in range(1000000):
shuffle(boxes) # Shake!
shuffle(boxes[0]) # Shake!
shuffle(boxes[1]) # Shake!
box = boxes[0] # Grab one of the two boxes
note = box[0] # Grab one of the two notes in the box
if note==100:
# If the note is 100 observe the other note and keep count of its denomination.
if box[1] == 1:
other_note_was_1 +=1
else:
other_note_was_100 += 1
else:
# Challenge doesn't tell us what to do here.
pass
probability = float(other_note_was_100) / (other_note_was_100 + other_note_was_1)
print(probability)
JavaScript
function shuffle(array) {
let currentIndex = array.length, randomIndex;
// While there remain elements to shuffle.
while (currentIndex != 0) {
// Pick a remaining element.
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}
return array;
}
let boxes = [ [100,100], [100,1]]
let other_note_was_1=0;
let other_note_was_100=0;
for (let i = 0; i < 1000000; i++) {
shuffle(boxes) //# Shake!
shuffle(boxes[0]); //# Shake!
shuffle(boxes[1]); //# Shake!
box = boxes[0]; //# Grab one of the two boxes
note = box[0]; //# Grab one of the two notes in the box
if (note==100) {
// # If the note is 100 observe the other note and keep count of its denomination.
if (box[1] == 1) {
other_note_was_1 +=1;
} else {
other_note_was_100 += 1;
}
} else {
// # Challenge doesn't tell us what to do here.
continue;
}
}
probability = (other_note_was_100) / (other_note_was_100 + other_note_was_1)
console.log(probability);
$('input').val(probability);