Created
August 20, 2018 13:11
-
-
Save funmaker/79deb81a2475fcbd19b14d41cab0079d to your computer and use it in GitHub Desktop.
Balls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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