weighted-selection 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 47780d3e6fcb74e347d8ed950eee0ff5851ee1f9
4
+ data.tar.gz: b49111c74cf339346a86ec4d0d233d6bb07e9ad8
5
+ SHA512:
6
+ metadata.gz: 690c3c510fd6193584ec9e1b27cca8b1a6a1b5103430cc5b1308a8056bb657b83c58087f02e064af18960a7f8a374a58198434de50699f683104135c04525c8d
7
+ data.tar.gz: 788ac75dd719eddf67084d811090d352f24cd5f0391018572da95af563e1c7491e2e5fb616812a36fea6c1fd7989f4563c4b471ef60db5a0e5af7ec55491fc89
data/LICENSE ADDED
@@ -0,0 +1,25 @@
1
+ This is free and unencumbered software released into the public domain.
2
+
3
+ Anyone is free to copy, modify, publish, use, compile, sell, or
4
+ distribute this software, either in source code form or as a compiled
5
+ binary, for any purpose, commercial or non-commercial, and by any
6
+ means.
7
+
8
+ In jurisdictions that recognize copyright laws, the author or authors
9
+ of this software dedicate any and all copyright interest in the
10
+ software to the public domain. We make this dedication for the benefit
11
+ of the public at large and to the detriment of our heirs and
12
+ successors. We intend this dedication to be an overt act of
13
+ relinquishment in perpetuity of all present and future rights to this
14
+ software under copyright law.
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 NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ For more information, please refer to <http://unlicense.org>
25
+
@@ -0,0 +1,51 @@
1
+ # Weighted selection
2
+
3
+ Simple weighted selection of items
4
+
5
+ ## Installation
6
+ Install [RAKE](https://rubygems.org/gems/weighted-selection) via RubyGem:
7
+
8
+ ```
9
+ gem install weighted-selection
10
+ ```
11
+
12
+ ## Usage
13
+
14
+ Import the library and create a new instance
15
+
16
+ ```
17
+ require 'weighted_selection'
18
+ sel = WeightedSelection::Engine.new
19
+ ```
20
+
21
+ ## Add items
22
+
23
+ Add single items to the selecter
24
+
25
+ ```
26
+ sel.add_item(WeightedSelection::Item.new('Item 1', 25)
27
+ sel.add_item(WeightedSelection::Item.new('Item 2', 25)
28
+ ```
29
+
30
+ Or multiple at once
31
+
32
+ ```
33
+ sel.add_items([
34
+ WeightedSelection::Item.new('Item 3', 25),
35
+ WeightedSelection::Item.new('Item 4', 25)
36
+ ])
37
+ ```
38
+
39
+ ## Fetch a sample
40
+
41
+ Fetch a sample based on the item's weights
42
+
43
+ ```
44
+ sel.select
45
+ ```
46
+
47
+ Or multiple at once
48
+
49
+ ```
50
+ sel.multiple(100)
51
+ ```
@@ -0,0 +1,3 @@
1
+ require_relative 'weighted_selection/version'
2
+ require_relative 'weighted_selection/engine'
3
+ require_relative 'weighted_selection/item'
@@ -0,0 +1,43 @@
1
+ module WeightedSelection
2
+ class Engine
3
+ def initialize
4
+ @items = []
5
+ end
6
+ def add_item(item)
7
+ @items.push(item)
8
+ end
9
+ def add_items(items)
10
+ items.each { |item| add_item(item) }
11
+ end
12
+ def sample
13
+ # Hold the weight sum
14
+ sum = 0
15
+
16
+ # Go through the items
17
+ @items.each { |item| sum += item.weight > 0 ? item.weight : 1 }
18
+
19
+ # Get random check-value
20
+ check = rand(sum)
21
+
22
+ # Loop items, again
23
+ @items.each do |item|
24
+ check -= item.weight
25
+ if check <= 0
26
+ return item
27
+ end
28
+ end
29
+
30
+ # Select an item at random
31
+ return items.sample
32
+ end
33
+ def multiple(count)
34
+ output = []
35
+ for i in 0..count
36
+ output.push(sample)
37
+ end
38
+ return output
39
+ end
40
+
41
+ attr_reader :items
42
+ end
43
+ end
@@ -0,0 +1,9 @@
1
+ module WeightedSelection
2
+ class Item
3
+ def initialize(weight, value)
4
+ @weight = weight
5
+ @value = value
6
+ end
7
+ attr_reader :weight, :value
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module WeightedSelection
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,14 @@
1
+ require 'minitest/autorun'
2
+ require 'weighted_selection'
3
+
4
+ class WeightedSelectionTest < MiniTest::Unit::TestCase
5
+ def test_add_items
6
+ sel = WeightedSelection::Engine.new
7
+ sel.add_items([
8
+ WeightedSelection::Item.new(50, 'Item 1'),
9
+ WeightedSelection::Item.new(50, 'Item 2')
10
+ ])
11
+
12
+ assert_equal(sel.items.length, 2)
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: weighted-selection
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Felix Lindström
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-08 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Simple, weighted selection of items.
14
+ email:
15
+ - felix.lindstrom@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - README.md
21
+ - LICENSE
22
+ - lib/weighted_selection/engine.rb
23
+ - lib/weighted_selection/item.rb
24
+ - lib/weighted_selection/version.rb
25
+ - lib/weighted_selection.rb
26
+ - test/test_weighted_selection.rb
27
+ homepage: https://github.com/felixlindstrom/weighted-selection
28
+ licenses: []
29
+ metadata: {}
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ required_rubygems_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 2.0.14
47
+ signing_key:
48
+ specification_version: 4
49
+ summary: Simple, weighted selection of items.
50
+ test_files:
51
+ - test/test_weighted_selection.rb