Last active
February 11, 2017 22:02
-
-
Save arcollector/ace7471c0a14a1520ca2ee68a815ed11 to your computer and use it in GitHub Desktop.
Tossing 2 coins - probability calculations
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
(() => { | |
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