Skip to content

Instantly share code, notes, and snippets.

@c-johnson
Created January 29, 2020 02:54
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 c-johnson/91c9f2f1c0574cfd05978188adff0b4d to your computer and use it in GitHub Desktop.
Save c-johnson/91c9f2f1c0574cfd05978188adff0b4d to your computer and use it in GitHub Desktop.
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)
@exussum12
Copy link

There is a bug in line 3. Changing 20 to 4 will fix this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment