Created
January 29, 2020 02:54
-
-
Save c-johnson/91c9f2f1c0574cfd05978188adff0b4d to your computer and use it in GitHub Desktop.
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
function coinflip() { | |
let string = ""; | |
for (var i = 0; i < 20; i++) { | |
let flip = Math.round(Math.random()); | |
string+= flip === 1 ? "H" : "T" | |
} | |
return string | |
} | |
function montecarlo(iters) { | |
let numHHHT = 0; | |
let numHHHH = 0; | |
for (var i = 0; i < iters; i++) { | |
let flipResult = coinflip(); | |
if (flipResult.indexOf("HHHT") !== -1) numHHHT++; | |
if (flipResult.indexOf("HHHH") !== -1) numHHHH++; | |
} | |
console.log("num HHHT = ", numHHHT) | |
console.log("num HHHH = ", numHHHH) | |
} | |
montecarlo(1000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There is a bug in line 3. Changing 20 to 4 will fix this