Skip to content

Instantly share code, notes, and snippets.

@arcollector
Last active February 11, 2017 22:02
Show Gist options
  • Save arcollector/ace7471c0a14a1520ca2ee68a815ed11 to your computer and use it in GitHub Desktop.
Save arcollector/ace7471c0a14a1520ca2ee68a815ed11 to your computer and use it in GitHub Desktop.
Tossing 2 coins - probability calculations
(() => {
const toss = () => Math.random() >= .5;
const throws = [];
for(let i = 0; i < 10000; ++i) {
const isHead1 = toss(),
isHead2 = toss();
throws.push(
isHead1 && isHead2 ? 'HH' :
!isHead1 && !isHead2 ? 'TT' :
'HT' // or TH
);
}
let hh = 0,
tt = 0,
ht = 0;
throws.forEach(
(x) => x == 'HH' ? ++hh :
x == 'TT' ? ++tt :
++ht
);
console.log(hh/10000,tt/10000,ht/10000);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment