weighted_shuffle 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ lib/**/*.rb
2
+ README.rdoc
3
+ ChangeLog.rdoc
4
+ LICENSE.txt
@@ -0,0 +1,5 @@
1
+ html/
2
+ pkg/
3
+ bin/
4
+ .bundle/
5
+ coverage/
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour --format documentation
@@ -0,0 +1,14 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - ruby-head
6
+ - jruby-19mode
7
+ - jruby-head
8
+ - rbx-19mode
9
+ - rbx-head
10
+ script: bundle exec rspec
11
+ matrix:
12
+ allow_failures:
13
+ - rvm: rbx-19mode
14
+ - rvm: rbx-head
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in yaga.gemspec
4
+ gemspec
@@ -0,0 +1,49 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ weighted_shuffle (0.1.0)
5
+ activesupport
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (3.2.13)
11
+ i18n (= 0.6.1)
12
+ multi_json (~> 1.0)
13
+ colorize (0.5.8)
14
+ coveralls (0.6.7)
15
+ colorize
16
+ multi_json (~> 1.3)
17
+ rest-client
18
+ simplecov (>= 0.7)
19
+ thor
20
+ diff-lcs (1.2.4)
21
+ i18n (0.6.1)
22
+ mime-types (1.23)
23
+ multi_json (1.7.6)
24
+ rest-client (1.6.7)
25
+ mime-types (>= 1.16)
26
+ rspec (2.13.0)
27
+ rspec-core (~> 2.13.0)
28
+ rspec-expectations (~> 2.13.0)
29
+ rspec-mocks (~> 2.13.0)
30
+ rspec-core (2.13.1)
31
+ rspec-expectations (2.13.0)
32
+ diff-lcs (>= 1.1.3, < 2.0)
33
+ rspec-mocks (2.13.1)
34
+ rubygems-tasks (0.2.4)
35
+ simplecov (0.7.1)
36
+ multi_json (~> 1.0)
37
+ simplecov-html (~> 0.7.1)
38
+ simplecov-html (0.7.1)
39
+ thor (0.18.1)
40
+
41
+ PLATFORMS
42
+ ruby
43
+
44
+ DEPENDENCIES
45
+ coveralls
46
+ rspec (~> 2.4)
47
+ rubygems-tasks (~> 0.2)
48
+ simplecov
49
+ weighted_shuffle!
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Frank C. Eckert
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,48 @@
1
+ # Weighted Shuffle
2
+
3
+ [![Build Status](https://travis-ci.org/opahk/weighted_shuffle.png?branch=master)](https://travis-ci.org/opahk/weighted_shuffle)
4
+ [![Coverage Status](https://coveralls.io/repos/opahk/weighted_shuffle/badge.png)](https://coveralls.io/r/opahk/weighted_shuffle)
5
+ [![Code Climate](https://codeclimate.com/github/opahk/weighted_shuffle.png)](https://codeclimate.com/github/opahk/weighted_shuffle)
6
+
7
+ An extension to the Fisher-Yates-Shuffle algorithm to support weights. It
8
+ includes a ruby core extension convenience of arrays.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'weighted_shuffle'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install weighted_shuffle
23
+
24
+ ## Usage
25
+
26
+ standalone usage:
27
+
28
+ array = [[:a, 1], [:b, 2]] # 1, 2 are weights
29
+ dealer = WeightedShuffle::Dealer.new array
30
+
31
+ dealer.weighted_shuffle
32
+ => [:b, :a]
33
+
34
+ integrated into ruby Array:
35
+
36
+ require 'weighted_shuffle/core_ext/array/weighted_shuffle'
37
+
38
+ array = [[:a, 1], [:b, 2]]
39
+ array.weighted_shuffle
40
+ => [:b, :a]
41
+
42
+ ## Contributing
43
+
44
+ 1. Fork it
45
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
46
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
47
+ 4. Push to the branch (`git push origin my-new-feature`)
48
+ 5. Create new Pull Request
@@ -0,0 +1,41 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'rake'
5
+
6
+ begin
7
+ gem 'rubygems-tasks', '~> 0.2'
8
+ require 'rubygems/tasks'
9
+
10
+ Gem::Tasks.new
11
+ rescue LoadError => e
12
+ warn e.message
13
+ warn "Run `gem install rubygems-tasks` to install Gem::Tasks."
14
+ end
15
+
16
+ begin
17
+ gem 'rdoc', '~> 3.0'
18
+ require 'rdoc/task'
19
+
20
+ RDoc::Task.new do |rdoc|
21
+ rdoc.title = "weighted_shuffle"
22
+ end
23
+ rescue LoadError => e
24
+ warn e.message
25
+ warn "Run `gem install rdoc` to install 'rdoc/task'."
26
+ end
27
+ task :doc => :rdoc
28
+
29
+ begin
30
+ gem 'rspec', '~> 2.4'
31
+ require 'rspec/core/rake_task'
32
+
33
+ RSpec::Core::RakeTask.new
34
+ rescue LoadError => e
35
+ task :spec do
36
+ abort "Please run `gem install rspec` to install RSpec."
37
+ end
38
+ end
39
+
40
+ task :test => :spec
41
+ task :default => :spec
@@ -0,0 +1,5 @@
1
+ require 'weighted_shuffle/version'
2
+ require 'weighted_shuffle/dealer'
3
+
4
+ module WeightedShuffle
5
+ end
@@ -0,0 +1,6 @@
1
+ class Array
2
+
3
+ def weighted_shuffle
4
+ WeightedShuffle::Dealer.new(self).weighted_shuffle
5
+ end
6
+ end
@@ -0,0 +1,25 @@
1
+ require 'active_support/core_ext/enumerable'
2
+
3
+ module WeightedShuffle
4
+ class Dealer < Struct.new(:array)
5
+
6
+ def weighted_shuffle
7
+ a = Marshal.load Marshal.dump(array)
8
+ sum = a.sum { |k,v| v || 0.0 }
9
+ b = []
10
+ a.length.times do
11
+ random = SecureRandom.random_number * sum
12
+ a.each_with_index do |(k,v),j|
13
+ if random <= v
14
+ b << a.delete_at(j).first
15
+ sum -= v
16
+ break
17
+ else
18
+ random -= v
19
+ end
20
+ end
21
+ end
22
+ b
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,4 @@
1
+ module WeightedShuffle
2
+ # weighted_shuffle version
3
+ VERSION = "0.1.0"
4
+ end
@@ -0,0 +1,25 @@
1
+ require 'simplecov'
2
+ require 'coveralls'
3
+
4
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
5
+ SimpleCov::Formatter::HTMLFormatter,
6
+ Coveralls::SimpleCov::Formatter
7
+ ]
8
+ SimpleCov.start do
9
+ add_filter '/spec/'
10
+ end
11
+
12
+ require 'rspec'
13
+ require 'weighted_shuffle'
14
+
15
+ RSpec.configure do |config|
16
+ config.treat_symbols_as_metadata_keys_with_true_values = true
17
+ config.run_all_when_everything_filtered = true
18
+ config.filter_run :focus
19
+
20
+ # Run specs in random order to surface order dependencies. If you find an
21
+ # order dependency and want to debug it, you can fix the order by providing
22
+ # the seed, which is printed after each run.
23
+ # --seed 1234
24
+ config.order = 'random'
25
+ end
@@ -0,0 +1,7 @@
1
+ require 'weighted_shuffle/core_ext/array/weighted_shuffle'
2
+
3
+ describe '#weighted_shuffle' do
4
+ it 'responds to weighted_shuffle' do
5
+ Array.new.should respond_to(:weighted_shuffle)
6
+ end
7
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ module WeightedShuffle
4
+ describe Dealer do
5
+ describe '#weighted_shuffle' do
6
+ [[ [[:a, 1], [:b, 2]], [:a, :b] ],
7
+ [ [[1, 1], [2, 3]], [1, 2] ],
8
+ [ [[2.0, 1], [:a, 1.5], ['a', 2.3]], [2.0, :a, 'a'] ]]
9
+ .each do |input, output|
10
+
11
+ it "returns a permutation of #{output} given #{input}" do
12
+ output.permutation.to_a.should include(
13
+ Dealer.new(input).weighted_shuffle)
14
+ end
15
+
16
+ it 'gives correct relative frequency for first position' do
17
+ weights = input.map(&:last)
18
+ # calculate probabiities
19
+ sum = weights.sum
20
+ probabilities = weights.map { |e| e.to_f / sum }
21
+
22
+ # establish frequencies
23
+ size = 1000
24
+ sample = size.times.map { Dealer.new(input).weighted_shuffle }
25
+ frequencies = input.map(&:first)
26
+ .map do |e|
27
+ sample.select { |s| s.first == e }.count.to_f / size
28
+ end
29
+
30
+ # calculate sum of absolute values of differences
31
+ diff = probabilities.zip(frequencies).map { |p,f| (p - f).abs }.sum
32
+ diff.should < 0.1
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.expand_path('../lib/weighted_shuffle/version', __FILE__)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "weighted_shuffle"
7
+ gem.version = WeightedShuffle::VERSION
8
+ gem.summary = %q{Fisher-Yates-Shuffle algorithm with weight support}
9
+ gem.description = <<-EOF
10
+ An extension to the Fisher-Yates-Shuffle algorithm to support weights. It
11
+ includes a ruby core extension for arrays.
12
+ EOF
13
+ gem.license = "MIT"
14
+ gem.authors = ["Frank C. Eckert"]
15
+ gem.email = "frank.eckert@boost-project.com"
16
+ gem.homepage = "https://rubygems.org/gems/weighted_shuffle"
17
+
18
+ gem.files = `git ls-files`.split($/)
19
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
20
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
21
+ gem.require_paths = ['lib']
22
+
23
+ gem.add_dependency 'activesupport'
24
+
25
+ gem.add_development_dependency 'rspec', '~> 2.4'
26
+ gem.add_development_dependency 'simplecov'
27
+ gem.add_development_dependency 'coveralls'
28
+ gem.add_development_dependency 'rubygems-tasks', '~> 0.2'
29
+ end
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: weighted_shuffle
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Frank C. Eckert
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
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: '2.4'
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: '2.4'
46
+ - !ruby/object:Gem::Dependency
47
+ name: simplecov
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
+ - !ruby/object:Gem::Dependency
63
+ name: coveralls
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rubygems-tasks
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: '0.2'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: '0.2'
94
+ description: ! " An extension to the Fisher-Yates-Shuffle algorithm to support weights.
95
+ \ It\n includes a ruby core extension for arrays.\n"
96
+ email: frank.eckert@boost-project.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .document
102
+ - .gitignore
103
+ - .rspec
104
+ - .travis.yml
105
+ - Gemfile
106
+ - Gemfile.lock
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - lib/weighted_shuffle.rb
111
+ - lib/weighted_shuffle/core_ext/array/weighted_shuffle.rb
112
+ - lib/weighted_shuffle/dealer.rb
113
+ - lib/weighted_shuffle/version.rb
114
+ - spec/spec_helper.rb
115
+ - spec/weighted_shuffle/core_ext/array/weighted_shuffle_spec.rb
116
+ - spec/weighted_shuffle/dealer_spec.rb
117
+ - weighted_shuffle.gemspec
118
+ homepage: https://rubygems.org/gems/weighted_shuffle
119
+ licenses:
120
+ - MIT
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ none: false
127
+ requirements:
128
+ - - ! '>='
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ none: false
133
+ requirements:
134
+ - - ! '>='
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubyforge_project:
139
+ rubygems_version: 1.8.24
140
+ signing_key:
141
+ specification_version: 3
142
+ summary: Fisher-Yates-Shuffle algorithm with weight support
143
+ test_files:
144
+ - spec/spec_helper.rb
145
+ - spec/weighted_shuffle/core_ext/array/weighted_shuffle_spec.rb
146
+ - spec/weighted_shuffle/dealer_spec.rb
147
+ has_rdoc: