Skip to content
Please note that GitHub no longer supports Internet Explorer.

We recommend upgrading to the latest Microsoft Edge, Google Chrome, or Firefox.

Learn more

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

exussum12 commented Jan 30, 2020

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
You can’t perform that action at this time.