wrandom 0.0.1

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/wrandom.rb +26 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ed64f70f455b763cf7cc7b3439f19e989775e2dc
4
+ data.tar.gz: 6b22952321638afe581db711720676f4a0a94b99
5
+ SHA512:
6
+ metadata.gz: 6b2c71ffa4d00441f1c07cae8250bd12ecda37592a11f14aebbb33f6e4eba1f50e7c0cdf28cca14a976ddbc184b0c919c53046a67a5cc66898cfdb45074d1e5b
7
+ data.tar.gz: 842907b3b50c0e3962ca3d3cb16789e59f873c3bb8ba4c6a41371070500e1de369ed5cd5027890b5d615dc6f89cc409a84dc8ee8457879f96537e132d039fb47
data/lib/wrandom.rb ADDED
@@ -0,0 +1,26 @@
1
+ class Array
2
+ def wsample(count = nil, &block)
3
+ ret = max_by(count || 1) { |v| w_algorithm(v, &block) }
4
+ count ? ret : ret[0]
5
+ end
6
+
7
+ def wshuffle(&block)
8
+ sort_by { |v| w_algorithm(v, &block) }
9
+ end
10
+
11
+ def wshuffle!(&block)
12
+ sort_by! { |v| w_algorithm(v, &block) }
13
+ end
14
+
15
+ private
16
+
17
+ def w_algorithm(v, &block)
18
+ # Pavlos S. Efraimidis, Paul G. Spirakis
19
+ # Weighted random sampling with a reservoir
20
+ # Information Processing Letters
21
+ # Volume 97 Issue 5, 16 March 2006
22
+ # Pages 181-185
23
+ population = block.call(v)
24
+ rand ** ( 1.0 / population )
25
+ end
26
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wrandom
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - juyeong
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-30 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: monkey patching Array class. (weighted shuffle, weighted sample)
14
+ email: dev@jy.is
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/wrandom.rb
20
+ homepage: https://github.com/juyeong/wrandom
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.4.5.1
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Weighted random and shuffle for array
44
+ test_files: []