Initial commit
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
function chooseWeightedRandom(items, weights) {
|
||||
let i;
|
||||
let length = weights.length;
|
||||
weights = [...weights];
|
||||
|
||||
for (i = 0; i < length; i++) weights[i] += weights[i - 1] || 0;
|
||||
|
||||
const random = Math.random() * weights[length - 1];
|
||||
|
||||
for (i = 0; i < length; i++) if (weights[i] > random) break;
|
||||
|
||||
return items[i];
|
||||
};
|
||||
|
||||
module.exports = chooseWeightedRandom;
|
||||
Reference in New Issue
Block a user