Skip to content

Instantly share code, notes, and snippets.

@funmaker
Created August 20, 2018 13:11
Show Gist options
  • Save funmaker/79deb81a2475fcbd19b14d41cab0079d to your computer and use it in GitHub Desktop.
Save funmaker/79deb81a2475fcbd19b14d41cab0079d to your computer and use it in GitHub Desktop.
Balls
let goldSilver = 0;
let goldGold = 0;
Array.prototype.random = function () {
return this.splice(Math.floor((Math.random()*this.length)), 1)[0];
}
for(let i = 0; i < 1000000; i++) {
let boxes = [["Gold", "Gold"], ["Gold", "Silver"], ["Silver", "Silver"]];
let randomBox = boxes.random();
let firstBall = randomBox.random();
if(firstBall !== "Gold") continue;
let secondBall = randomBox.random();
if(secondBall === "Gold") goldGold++;
else goldSilver++;
}
console.log("Gold -> Gold: ", goldGold)
console.log("Gold -> Silver: ", goldSilver)
console.log("Probability: ", Math.round(goldGold / (goldGold + goldSilver) * 100) + "%")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment