Weighted Random Distribution
A simple utility module to select from a set of discrete values based on weighted random distribution.
Table of Contents
inPlaceUnsorted()
Perform an in-place unsorted weighted value selection. (The algorithm is ported from Python code here).
Parameters
- weights {object} - An Object with each key representing one of the discrete values that will be selected, and each value representing a weight.
Returns
One of the keys from the weights param, with the probability of a key being returned based on the weight values {string}
Example
// output: 70% chance of 'a', 20% chance of 'b', 10 percent chance of 'c' // output: 50% change of 'a', 25% chance of 'b', 25% chance 'c'
Numeric values don't need to sum to any particular number, and can be decimal.
normalize()
A utility method to provide min-max normalization for input weights. Given a starting range of xMin and xMax, returns valueToNormalize mapped to the same relative position in relation to transformedMin and transformedMax. Defaults to a transformed range between 0 and 1.
Parameters
- valueToNormalize {number}
- xMin {number} The floor of the normalization range.
- xMax {number} The ceiling of the normalization range.
- transformedMin {number} Default value is 0.
- transformedMax {number} Default value is 1.
Returns
The normalized representation of valueToNormalize {number}.
Examples
;// output: .5 ;// output: 4 ;// output: 1 ;// output: 30 ;// output: 40