weightedSample
Gets a random element from an array, using the provided weights as the probabilities for each element.
- Use
Array.prototype.reduce()to create an array of partial sums for each value inweights. - Use
Math.random()to generate a random number andArray.prototype.findIndex()to find the correct index based on the array previously produced. - Finally, return the element of
arrwith the produced index.