Select an item based on weight.
reducethresholdfunction pick(items, random) {
const total = items.reduce((s, i) => s + i.weight, 0);
let threshold = random * total;
for (const i of items) {
threshold -= i.weight;
if (threshold <= 0) return i.val;
}
}