weighted-list
A vector implementation for weighted randomisation.
Implements the WeightedList struct, an ordered collection of WeightedItems, which hold a value and weight. Indexing and random selection takes the weight of items into consideration, such that items with greater weights are more likely to be selected.
Usage
Import
// blanket import
use *;
// specific imports
use ;
Construction
// macro
let wl = wlist!;
// constructor
let wl = init;
Weighted Randomisation
let descriptors = wlist!;
// single selection
let word = descriptors.select_random_value;
if let Some = word
// multiple selection (bon builder syntax)
let words = descriptors.select_random_values
.rng
.count
.unique
.call;
if let Some = words && let Some = words
Indexing
let wl = wlist!;
let _ = wl; // => WeightedItem { weight: 1, value: "qi" }
let _ = wl; // => WeightedItem { weight: 2, value: "sup" }
let _ = wl; // => WeightedItem { weight: 2, value: "sup" }
let _ = wl; // => WeightedItem { weight: 5, value: "shard" }
let _ = wl; // => WeightedItem { weight: 5, value: "shard" }
let _ = wl; // => WeightedItem { weight: 5, value: "shard" }
let _ = wl; // => WeightedItem { weight: 5, value: "shard" }
let _ = wl; // => WeightedItem { weight: 5, value: "shard" }
let _ = wl; // => panic - out of bounds!