Last active
January 10, 2022 03:29
-
-
Save mitchallen/9d523f87ab2ddf97ad83a2521796aa5e to your computer and use it in GitHub Desktop.
Test Weighted Coin Flip
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
| // 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