| 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
This comment has been minimized.
exussum12 commentedJan 30, 2020
There is a bug in line 3. Changing 20 to 4 will fix this