Skip to content

Instantly share code, notes, and snippets.

@gengkev
Last active December 26, 2015 17:29
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 gengkev/7187335 to your computer and use it in GitHub Desktop.
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!
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