0.0
No commit activity in last 3 years
No release in over 3 years
Gem for selecting items by weight
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.10
~> 10.0
>= 0
 Project Readme

Weighted select

Allows you to store random items with their weight and select them later based on weights.

Usage

require 'weighted-select'

# a/b/c/d relate as 10/4/1/16
weights  = { a: 10, b: 4, c: 1, d: 16 }
selector = WeightedSelect::Selector.new weights

# One can add some more items later
selector.add :e, 5
selector.add :f, 3

# One can increase weight by adding the same item again
selector.add :c, 4

selector.weights 
# { 
#   (0...10)  => :a, 
#   (10...14) => :b, 
#   (14...15) => :c, 
#   (15...31) => :d, 
#   (31...36) => :e, 
#   (36...39) => :f, 
#   (39...43) => :c
# }

# Returns random item based on weights 
selector.select

Anything can be an item. Weight must be positive integer. Total weight of all items is stored in selector.total_weight.