Created
December 8, 2020 21:21
-
-
Save brandonstephens/c407d63a6cced0c1984a6c812362ac3e to your computer and use it in GitHub Desktop.
Testing out simple probability stuff
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
// Playground for testing simple probability | |
// more cycles more consistent the output | |
let cycles = 1000; | |
// how often do you want value to return true | |
// 0 === never, 1 === always | |
let truthAmount = 0.7; | |
// run the test | |
let output = []; | |
while (cycles > 0) { | |
output.push(Math.random() < truthAmount); | |
cycles = cycles - 1; | |
} | |
// log how often it was true | |
console.log( | |
output.reduce((acc, val) => { | |
acc = val ? acc + 1 : acc; | |
return acc; | |
}, 0) / output.length | |
); | |
// example of flipping a coin | |
const flipCoin = (Math.random() < 0.5) ? 'heads' : 'tails' | |
// example of a dice rolling a 6 | |
const roll = (Math.random() < 0.16) ? '6' : 'not a 6' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment