weighted_distribution 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/weighted_distribution.rb +7 -6
  3. metadata +5 -10
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c9bb5df8a285b645b8a36b31668dadf1166b55f3
4
+ data.tar.gz: 8298252b04f543bd69882a143b383c3b02bc9609
5
+ SHA512:
6
+ metadata.gz: 920a1dcf8422e436c035b6ac1dbd638af3d241112afdf1c68c201f4532a373ae5a26e4b8392552c33efd175decca2292816464c11cc03487e93507f45a5b822b
7
+ data.tar.gz: c3b551c34ff9ca4bea44abc29f4072fcd0db62390e66e65dd55dbe9b05a1399b93670617ebb893dae765e2fc4db85d533d23cec62143e195cc7690b1d2a29bf7
@@ -2,8 +2,8 @@
2
2
  # Constructor expects a hash of object => weight pairs where the
3
3
  # weight represents the probability of the corresponding
4
4
  # object being selected. follows specification of ryanlecompte's
5
- # WeightedDistributionizer[link:https://github.com/ryanlecompte/weighted_randomizer]
6
- # reimplements the sample function to run in O(log(n)) rather than O(n).
5
+ # WeightedRandomizer[link:https://github.com/ryanlecompte/weighted_randomizer]
6
+ # re-implements the sample function to run in O(log(n)) rather than O(n).
7
7
  #
8
8
  # @example Usage
9
9
  # suit_dist = WeightedDistribution.new({heart: 12, spade: 11, diamond: 10, club:13})
@@ -12,17 +12,18 @@
12
12
  # @note Mostly adapted from recipe 5.11 from the Ruby Cookbook.
13
13
  class WeightedDistribution
14
14
 
15
- # Creates a new instance of the WeightedDistributionizer Class
15
+ # Creates a new instance of the WeightedDistribution Class
16
16
  #
17
17
  # @param [Hash] object_weights objects with their corresponding
18
18
  # weights(key object, value weight)
19
- # @return [WeightedDistributionizer]
20
- def initialize(object_weights)
19
+ # @return [WeightedDistribution]
20
+ def initialize(object_weights, randomizer = Kernel)
21
21
  @len = object_weights.length
22
22
  @keys = object_weights.keys
23
23
  @orig_values = @keys.map{|x| object_weights[x]}
24
24
  @values = normalize(@orig_values)
25
25
  @csum = cumulative_sum(@values)
26
+ @randomizer = randomizer
26
27
  end
27
28
 
28
29
  # Samples from the WeightedDistribution. Each object has a
@@ -45,7 +46,7 @@ class WeightedDistribution
45
46
  #
46
47
  # @return [Object] sampled object from distribution
47
48
  def _sample
48
- random = Kernel.rand
49
+ random = @randomizer.rand
49
50
  lhs, rhs = 0, @len - 1
50
51
 
51
52
  while rhs >= lhs
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: weighted_distribution
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
5
- prerelease:
4
+ version: 1.1.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Jonathan Swenson
@@ -18,13 +17,11 @@ dependencies:
18
17
  - - '>='
19
18
  - !ruby/object:Gem::Version
20
19
  version: '0'
21
- none: false
22
20
  requirement: !ruby/object:Gem::Requirement
23
21
  requirements:
24
22
  - - '>='
25
23
  - !ruby/object:Gem::Version
26
24
  version: '0'
27
- none: false
28
25
  prerelease: false
29
26
  type: :development
30
27
  - !ruby/object:Gem::Dependency
@@ -34,13 +31,11 @@ dependencies:
34
31
  - - '>='
35
32
  - !ruby/object:Gem::Version
36
33
  version: '0'
37
- none: false
38
34
  requirement: !ruby/object:Gem::Requirement
39
35
  requirements:
40
36
  - - '>='
41
37
  - !ruby/object:Gem::Version
42
38
  version: '0'
43
- none: false
44
39
  prerelease: false
45
40
  type: :development
46
41
  description: Sample a weighted distribution of objects.
@@ -53,6 +48,7 @@ files:
53
48
  homepage: http://github.com/jonathanswensonsc/weighted_distribution
54
49
  licenses:
55
50
  - MIT
51
+ metadata: {}
56
52
  post_install_message:
57
53
  rdoc_options: []
58
54
  require_paths:
@@ -62,17 +58,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
62
58
  - - '>='
63
59
  - !ruby/object:Gem::Version
64
60
  version: '0'
65
- none: false
66
61
  required_rubygems_version: !ruby/object:Gem::Requirement
67
62
  requirements:
68
63
  - - '>='
69
64
  - !ruby/object:Gem::Version
70
65
  version: '0'
71
- none: false
72
66
  requirements: []
73
67
  rubyforge_project:
74
- rubygems_version: 1.8.24
68
+ rubygems_version: 2.1.9
75
69
  signing_key:
76
- specification_version: 3
70
+ specification_version: 4
77
71
  summary: Sample a weighted distribution of objects.
78
72
  test_files: []
73
+ has_rdoc: