No commit activity in last 3 years
No release in over 3 years
Sample a weighted distribution of objects.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
>= 0
 Project Readme

Weighted Distribution

Weighted Distribution is used to sample from a distribution of objects with different weights. Sampling from this distribution will randomly select an object with a probability proportional to its weight. I followed the specification used by Ryan Lecompte's Weighted Randomizer. I have improved on the runtime efficiency of the sampling algorithm to run in O(log(n)) time rather then O(n). This will be useful for higher performance distribution sampling from distributions with many objects.

Installation

Add this line to your application's Gemfile:

gem 'weighted_distribution'

And then execute:

$ bundle

Or install it yourself as:

$ gem install weighted_distribution

Usage

To sample from a distribution:

  suits = {'heart' => 12, 'spade' => 11, 'diamond' => 10, 'club' => 13}
  suit_distribution = WeightedDistribution.new(suits)

  # Fetch a single random object.
  suit_distribution.sample # => single object

  # Fetch the next 10 weighted random objects.
  suit_distribution.sample(10) # => array of objects

License

Licensed with the MIT license (see LICENSE)

Acknowledgements

The design of the gem and its specification was based largely on Ryan Lecompte's Weighted Randomizer which was based on recipe 5.11 from the Ruby Cookbook