Skip to content

Instantly share code, notes, and snippets.

@brandonstephens
Created December 8, 2020 21:21
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 brandonstephens/c407d63a6cced0c1984a6c812362ac3e to your computer and use it in GitHub Desktop.
Save brandonstephens/c407d63a6cced0c1984a6c812362ac3e to your computer and use it in GitHub Desktop.
Testing out simple probability stuff
// 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