Last active
January 13, 2022 03:07
-
-
Save mitchallen/eefaac9dba23c9f8484eab0e538f9ea1 to your computer and use it in GitHub Desktop.
Test file for weighted choice
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-choice.js | |
| import { weightedChoice } from './weighted-choice.js'; | |
| function testWeightedChoice(source = {}) { | |
| console.log('\nSOURCE:'); | |
| console.log(source); | |
| // define the number of dice rolls | |
| const LIMIT = 100; | |
| // create an array filled with random results | |
| let arr = Array.from({ length: LIMIT }, () => weightedChoice(source)); | |
| // log the generated results | |
| console.log(arr); | |
| // count the occurences of each result | |
| let occurrences = arr.reduce((prev, curr) => (prev[curr] = ++prev[curr] || 1, prev), {}); | |
| // log a summary of the occurences | |
| console.log('\OCCURRENCES:'); | |
| console.log(occurrences); | |
| } | |
| testWeightedChoice({ | |
| "A": 0.25, | |
| "B": 0.50, | |
| "C": 0.25, | |
| }); | |
| testWeightedChoice({ | |
| "A": 0.50, | |
| "B": 0.25, | |
| "C": 0.25, | |
| }); | |
| testWeightedChoice({ | |
| "A": 0.50, | |
| "B": 0.35, | |
| "C": 0.10, | |
| "D": 0.10, | |
| }); | |
| testWeightedChoice({ | |
| "#000000": 0.50, | |
| "#FFFFFF": 0.35, | |
| "#FF0000": 0.10, | |
| "#0000FF": 0.10, | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment