Weighted selection
Simple weighted selection of items
Installation
Install RAKE via RubyGem:
gem install weighted-selection
Usage
Import the library and create a new instance
require 'weighted_selection'
sel = WeightedSelection::Engine.new
Add items
Add single items to the selecter
sel.add_item(WeightedSelection::Item.new('Item 1', 25)
sel.add_item(WeightedSelection::Item.new('Item 2', 25)
Or multiple at once
sel.add_items([
WeightedSelection::Item.new('Item 3', 25),
WeightedSelection::Item.new('Item 4', 25)
])
Fetch a sample
Fetch a sample based on the item's weights
sel.select
Or multiple at once
sel.multiple(100)