Last active
December 26, 2015 17:29
-
-
Save gengkev/7187335 to your computer and use it in GitHub Desktop.
a simulation for discussion 11 problem 3 in data analysis packet B, tjhsst 2013-14. it generates 4000 heads/tails combinations and counts how many appear consecutively, and how many times. to run, copy and paste this into a javascript console to run, i wrote it rather quickly!
This file contains hidden or 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
var arr = []; | |
for (var i = 0; i < 4000; i++) { | |
if (Math.random() < 0.5) arr.push("H"); | |
else arr.push("T"); | |
} | |
var str = arr.join(""); | |
var bla = str.match(/(H+|T+)/g); | |
var tab = {}; | |
for (var i = 0; i < bla.length; i++) { | |
if (typeof tab[bla[i]] == "undefined") { | |
tab[bla[i]] = 1; | |
} else { | |
tab[bla[i]]++; | |
} | |
} | |
console.log(tab); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment