Sha256: 2b91b98116342ed108aff64491a34714fa08af78d809501602174b138f759ef7
Contents?: true
Size: 879 Bytes
Versions: 4
Compression:
Stored size: 879 Bytes
Contents
(function($, corkboard) { // Ported to JS from weighted_randomizer Ruby gem. function WeightedRandomizer(setup) { this.normalize(setup); } // Surfaced as part of the corkboard namespace. corkboard.WeightedRandomizer = WeightedRandomizer; WeightedRandomizer.prototype.normalize = function normalize(setup) { var dict = {}; var sum = 0.0; $.each(setup, function(key, weight) { sum += weight; }); $.each(setup, function(key, weight) { dict[key] = (weight / sum); }); this.normalized = dict; }; WeightedRandomizer.prototype.sample = function sample() { var pick = Math.random(); var result; $.each(this.normalized, function(key, weight) { result = key; if(pick <= weight) { return false; } pick -= weight; }); return result; }; })(jQuery, jQuery.corkboard);
Version data entries
4 entries across 4 versions & 1 rubygems