loaded_die 1.0.1 → 1.0.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 362b5a5dc03dcbf3580aeb5501c964cb273a183ce9ab3763bc9840288f4e4b95
4
- data.tar.gz: e5e901d9a0611c2579d33595569658d112736f89714655db4b161349c5f00d24
3
+ metadata.gz: b04718fe3728aa1a6f354e81eed37708842a79fb62b6151f6d9a2823bccd603e
4
+ data.tar.gz: a6993994255411fdcd889aab140e8c8a1cb8453fad9ad44fb972076330409534
5
5
  SHA512:
6
- metadata.gz: 5510423a7c21354514d3a035f77b7c518d2ed563ac315dc1d1ebba6f10459201416a5ed427cadb9c78c957a94a591a9766c75dabe8b15ca96911e8aed413350b
7
- data.tar.gz: 5d8ccf6c0f9713cf6e1d3bf1b45ba63d7c026d7e41523e0c61211163815173cdeff20341b5c4518350674ecc0f67565de16f29dd1f063a7dc2d347e9084c01f7
6
+ metadata.gz: 52d5caf5851d300a185600d5a6e00203bebf672913a78ab42ca4430a369b5d690033fd679ccbef1df8abac7bbdd6c63e77d8f04067f4445142caf4b052e0850c
7
+ data.tar.gz: 2d896e03013c765bfe6beec383ffa456e9e4c199573d8b77900d3a7badef92f70d0a90a49e467317b98308b6779246c34e52401aec1d602b7e59e844071ab94b
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2018 Aaron Beckerman
1
+ Copyright (c) 2022 Aaron Beckerman
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy of
4
4
  this software and associated documentation files (the "Software"), to deal in
data/README.rdoc CHANGED
@@ -1,7 +1,7 @@
1
1
  = Loaded Die
2
2
 
3
3
  Loaded Die is a Ruby library that makes it easy to randomly choose from a set
4
- of options when some options are more likely than others.
4
+ of options where some options are more likely than others.
5
5
 
6
6
 
7
7
  == Usage
@@ -33,8 +33,8 @@ sampler like this:
33
33
 
34
34
  You can specify the random number generator that sample uses. To do this,
35
35
  supply as the argument a hash with your random number generator under the
36
- :random key. (This is based on the behavior of Ruby's Array#sample.) Your
37
- random number generator will be sent the "rand" message with one argument, the
36
+ +:random+ key. (This is based on the behavior of Ruby's Array#sample.) Your
37
+ random number generator will be sent a "rand" message with one argument, the
38
38
  sum of the weights you specified when creating the sampler. The return value
39
39
  must be a number greater than or equal to zero and less than the sum of the
40
40
  weights.
data/lib/loaded_die.rb CHANGED
@@ -1,14 +1,42 @@
1
+ ##
2
+ # This module provides a class for randomly choosing from a set of options
3
+ # where some options are more likely than others.
4
+ #
1
5
  module LoadedDie
2
- VERSION = '1.0.1'
3
6
 
7
+ ##
8
+ # The version string.
9
+ #
10
+ VERSION = '1.0.2'
11
+
12
+ ##
13
+ # Objects of this class can choose randomly from a set of options (called
14
+ # individuals here). The options can have different probabilities of being
15
+ # chosen.
16
+ #
4
17
  class Sampler
18
+
19
+ ##
20
+ # The default random number generator.
21
+ #
5
22
  DEFAULT_RNG = ::Object.new
6
23
  def DEFAULT_RNG.rand(n)
7
24
  ::Kernel.rand * n
8
25
  end
9
26
 
27
+ ##
28
+ # A class representing a segment of the number line with an associated
29
+ # individual.
30
+ #
10
31
  Segment = ::Struct.new(:maximum, :individual)
11
32
 
33
+ ##
34
+ # Creates a sampler from a population. The argument should be an enumerable
35
+ # of two-element arrays (a hash qualifies). The first element of each array
36
+ # is an individual that can be chosen; the second element is its weight --
37
+ # that is, the likelihood (relative to the other weights) that the
38
+ # individual will be chosen. Weights must be positive numbers.
39
+ #
12
40
  def initialize(population)
13
41
  @compiled = population.inject [] do |accum, (individual, weight)|
14
42
  if weight <= 0
@@ -25,6 +53,9 @@ module LoadedDie
25
53
  nil
26
54
  end
27
55
 
56
+ ##
57
+ # Returns the sum of weights.
58
+ #
28
59
  def length
29
60
  if last = @compiled.last
30
61
  last.maximum
@@ -33,6 +64,12 @@ module LoadedDie
33
64
  end
34
65
  end
35
66
 
67
+ ##
68
+ # Returns the individual for the given point. The point should be a number.
69
+ # If it is greater than or equal to zero and less than the sum of weights,
70
+ # this returns the corresponding individual. If it is outside those bounds,
71
+ # this returns nil.
72
+ #
36
73
  def [](point)
37
74
  if point < 0
38
75
  nil
@@ -43,6 +80,14 @@ module LoadedDie
43
80
  end
44
81
  end
45
82
 
83
+ ##
84
+ # Returns a randomly-chosen individual. The argument is a hash, empty by
85
+ # default. If it contains a value for the +:random+ key, that value will
86
+ # be used as the random number generator instead of the default. This RNG
87
+ # will be sent a "rand" message with one argument, the sum of weights, and
88
+ # should return a number greater than or equal to zero and less than the
89
+ # sum of weights.
90
+ #
46
91
  def sample(options = {})
47
92
  rng = options.fetch(:random) { DEFAULT_RNG }
48
93
  point = rng.rand(length)
data/loaded_die.gemspec CHANGED
@@ -7,8 +7,8 @@ Gem::Specification.new do |s|
7
7
  s.name = "loaded_die"
8
8
  s.version = LoadedDie::VERSION
9
9
  s.author = "Aaron Beckerman"
10
- s.summary = %q{A library for choosing randomly when some options are more likely than others.}
11
- s.description = %q{Loaded Die makes it easy to choose randomly when some options are more likely than others.}
10
+ s.summary = %q{A library for choosing randomly where some options are more likely than others.}
11
+ s.description = %q{Loaded Die is a library that makes it easy to choose randomly from a set of options where some options are more likely than others.}
12
12
  s.license = "MIT"
13
13
  s.platform = Gem::Platform::RUBY
14
14
  s.required_ruby_version = ">= 1.8.7"
@@ -27,11 +27,9 @@ lambda do
27
27
  sampler.respond_to?(:sample) or fail
28
28
  end.call
29
29
 
30
- lambda do
31
- 0 == LoadedDie::Sampler.new({}).length or fail
32
- 2 == LoadedDie::Sampler.new({ :a => 2 }).length or fail
33
- (2 + 3.1) == LoadedDie::Sampler.new({ :a => 2, :b => 3.1 }).length or fail
34
- end.call
30
+ 0 == LoadedDie::Sampler.new({}).length or fail
31
+ 2 == LoadedDie::Sampler.new({ :a => 2 }).length or fail
32
+ (2 + 3.1) == LoadedDie::Sampler.new({ :a => 2, :b => 3.1 }).length or fail
35
33
 
36
34
  lambda do
37
35
  sampler = LoadedDie::Sampler.new({})
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: loaded_die
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Beckerman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-31 00:00:00.000000000 Z
11
+ date: 2022-12-01 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: Loaded Die makes it easy to choose randomly when some options are more
14
- likely than others.
13
+ description: Loaded Die is a library that makes it easy to choose randomly from a
14
+ set of options where some options are more likely than others.
15
15
  email:
16
16
  executables: []
17
17
  extensions: []
@@ -41,10 +41,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
41
41
  - !ruby/object:Gem::Version
42
42
  version: '0'
43
43
  requirements: []
44
- rubyforge_project:
45
- rubygems_version: 2.7.6
44
+ rubygems_version: 3.1.6
46
45
  signing_key:
47
46
  specification_version: 4
48
- summary: A library for choosing randomly when some options are more likely than others.
47
+ summary: A library for choosing randomly where some options are more likely than others.
49
48
  test_files:
50
49
  - test/loaded_die_test.rb