Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save mitchallen/eefaac9dba23c9f8484eab0e538f9ea1 to your computer and use it in GitHub Desktop.
Test file for weighted choice
// 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