weighted_randomizer 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.yardopts +6 -0
- data/Changes.md +3 -0
- data/Gemfile +2 -0
- data/LICENSE +22 -0
- data/README.md +59 -0
- data/Rakefile +2 -0
- data/lib/weighted_randomizer.rb +58 -0
- data/spec/spec_helper.rb +5 -0
- data/spec/weighted_randomizer_spec.rb +31 -0
- data/weighted_randomizer.gemspec +22 -0
- metadata +113 -0
data/.gitignore
ADDED
data/.yardopts
ADDED
data/Changes.md
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Ryan LeCompte
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
# Weighted Randomizer
|
2
|
+
|
3
|
+
Weighted Randomizer is an implementation of weighted randomization in Ruby.
|
4
|
+
This gem is useful for situations where you have a group of items with
|
5
|
+
corresponding weights, and you want to randomly select an item with its associated
|
6
|
+
weight taken into account. Potential uses of this gem could be weighted job
|
7
|
+
queues or other types of data where weights are potentially important.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'weighted_randomizer'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install weighted_randomizer
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Using this gem is as simple as the following:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
queues = {'queue1' => 25, 'queue2' => 100, 'queue3' => 2}
|
29
|
+
randomizer = WeightedRandomizer.new(queues)
|
30
|
+
|
31
|
+
# Fetch a single random item.
|
32
|
+
randomizer.sample # => single item
|
33
|
+
|
34
|
+
# Fetch the next 10 weighted random items.
|
35
|
+
randomizer.sample(10) # => array of items
|
36
|
+
```
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
Please see LICENSE for licensing details.
|
41
|
+
|
42
|
+
## Author
|
43
|
+
|
44
|
+
Ryan LeCompte
|
45
|
+
|
46
|
+
[@ryanlecompte](https://twitter.com/ryanlecompte)
|
47
|
+
|
48
|
+
## Acknowledgements
|
49
|
+
|
50
|
+
The core of this gem was graciously based on recipe 5.11 from the
|
51
|
+
[Ruby Cookbook](http://shop.oreilly.com/product/9780596523695.do)
|
52
|
+
|
53
|
+
## Contributing
|
54
|
+
|
55
|
+
1. Fork it
|
56
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
57
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
58
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
59
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# Implements weighted randomization for a group of weighted items.
|
2
|
+
# This class expects a hash of key -> value pairs where the value is
|
3
|
+
# the weight for the item.
|
4
|
+
#
|
5
|
+
# @example Usage
|
6
|
+
# wr = WeightedRandomizer.new('queue1' => 25, 'queue2' => 100, 'queue3' => 2)
|
7
|
+
# puts "Using queue #{wr.sample}"
|
8
|
+
#
|
9
|
+
# @note Mostly adapted from recipe 5.11 from the Ruby Cookbook.
|
10
|
+
class WeightedRandomizer
|
11
|
+
VERSION = '0.1.0'
|
12
|
+
|
13
|
+
# Creates a new instance.
|
14
|
+
#
|
15
|
+
# @param [Hash] items the weighted items (key item, value weight)
|
16
|
+
# @return [WeightedRandomizer]
|
17
|
+
def initialize(items)
|
18
|
+
@items = normalize(items)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Returns one or more weighted random values.
|
22
|
+
#
|
23
|
+
# @param [Integer] num the number of samples to return
|
24
|
+
# @return [Object, Array<Object>] one or more sampled items
|
25
|
+
def sample(num = nil)
|
26
|
+
return _sample unless num
|
27
|
+
Array.new(num) { _sample }
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
# Returns a single weighted random value.
|
33
|
+
#
|
34
|
+
# @return [Object] the weighted item
|
35
|
+
def _sample
|
36
|
+
pick = rand
|
37
|
+
@items.each do |key, weight|
|
38
|
+
return key if pick <= weight
|
39
|
+
pick -= weight
|
40
|
+
end
|
41
|
+
nil
|
42
|
+
end
|
43
|
+
|
44
|
+
# Normalizes the weights to float values so that
|
45
|
+
# arbitrary integer/float weights can be specified.
|
46
|
+
#
|
47
|
+
# @param [Hash] items the weighted items (key item, value weight)
|
48
|
+
# @return [Hash] the items with their weights normalized
|
49
|
+
def normalize(items)
|
50
|
+
normalized = {}
|
51
|
+
sum = items.values.inject(0.0, :+)
|
52
|
+
items.each do |key, weight|
|
53
|
+
normalized[key] = weight / sum
|
54
|
+
end
|
55
|
+
|
56
|
+
normalized
|
57
|
+
end
|
58
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe WeightedRandomizer do
|
4
|
+
let(:items) { {:a => 2, :b => 5, :c => 53} }
|
5
|
+
let(:randomizer) { WeightedRandomizer.new(items) }
|
6
|
+
|
7
|
+
describe '#initialize' do
|
8
|
+
it 'creates a new instance with normalized weighted items' do
|
9
|
+
internal_items = randomizer.instance_variable_get(:@items)
|
10
|
+
internal_items.keys.should =~ items.keys
|
11
|
+
internal_items.values.inject(:+).should == 1.0
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '#sample' do
|
16
|
+
it 'returns a single item by default' do
|
17
|
+
randomizer.sample.should be_a(Symbol)
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'returns an array of items with count specified' do
|
21
|
+
randomizer.sample(5).should be_an(Array)
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'respects weights' do
|
25
|
+
result = randomizer.sample(1000).each_with_object(Hash.new(0)) { |i, h|
|
26
|
+
h[i] += 1
|
27
|
+
}
|
28
|
+
result.max_by { |k, v| v }.first.should == :c
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
require File.expand_path('../lib/weighted_randomizer', __FILE__)
|
4
|
+
|
5
|
+
Gem::Specification.new do |gem|
|
6
|
+
gem.authors = ["Ryan LeCompte"]
|
7
|
+
gem.email = ["lecompte@gmail.com"]
|
8
|
+
gem.description = %q{Provides a common utility for weighted randomization}
|
9
|
+
gem.summary = %q{Provides a common utility for weighted randomization}
|
10
|
+
gem.homepage = "http://github.com/ryanlecompte/weighted_randomizer"
|
11
|
+
|
12
|
+
gem.files = `git ls-files`.split($\)
|
13
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
14
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
15
|
+
gem.name = "weighted_randomizer"
|
16
|
+
gem.require_paths = ["lib"]
|
17
|
+
gem.version = WeightedRandomizer::VERSION
|
18
|
+
|
19
|
+
gem.add_development_dependency('rake')
|
20
|
+
gem.add_development_dependency('rspec')
|
21
|
+
gem.add_development_dependency('yard')
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: weighted_randomizer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ryan LeCompte
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-29 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: yard
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: Provides a common utility for weighted randomization
|
63
|
+
email:
|
64
|
+
- lecompte@gmail.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- .yardopts
|
71
|
+
- Changes.md
|
72
|
+
- Gemfile
|
73
|
+
- LICENSE
|
74
|
+
- README.md
|
75
|
+
- Rakefile
|
76
|
+
- lib/weighted_randomizer.rb
|
77
|
+
- spec/spec_helper.rb
|
78
|
+
- spec/weighted_randomizer_spec.rb
|
79
|
+
- weighted_randomizer.gemspec
|
80
|
+
homepage: http://github.com/ryanlecompte/weighted_randomizer
|
81
|
+
licenses: []
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ! '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
segments:
|
93
|
+
- 0
|
94
|
+
hash: -345244177172627536
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ! '>='
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
segments:
|
102
|
+
- 0
|
103
|
+
hash: -345244177172627536
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 1.8.23
|
107
|
+
signing_key:
|
108
|
+
specification_version: 3
|
109
|
+
summary: Provides a common utility for weighted randomization
|
110
|
+
test_files:
|
111
|
+
- spec/spec_helper.rb
|
112
|
+
- spec/weighted_randomizer_spec.rb
|
113
|
+
has_rdoc:
|