Skip to content

Instantly share code, notes, and snippets.

@mitchallen
Last active January 10, 2022 03:29
Show Gist options
  • Select an option

  • Save mitchallen/9d523f87ab2ddf97ad83a2521796aa5e to your computer and use it in GitHub Desktop.

Select an option

Save mitchallen/9d523f87ab2ddf97ad83a2521796aa5e to your computer and use it in GitHub Desktop.
Test Weighted Coin Flip
// Author: Mitch Allen
// File: test-weighted-coinflip.js
import {weightedCoinFlip} from './weighted-coinflip.js';
// define test function for weightedCoinFlip()
function testWeightedCoinFlip() {
// define the number of weighted coin flips to generate
const LIMIT = 100;
// define the weight or chance of true being returned
const WEIGHT = 0.1;
// create an array filled with weighted coin flip results
let arr = Array.from({length: LIMIT}, () => weightedCoinFlip( WEIGHT ));
// log the generated weighted coin flips
console.log(arr);
// count the occurences of 1s and 0s
let occurrences = arr.reduce((prev, curr) => (prev[curr] = ++prev[curr] || 1, prev), {});
// log a summary of the occurences
console.log(occurrences);
}
// call test function for weightedCoinFlip()
testWeightedCoinFlip();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment