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 +4 -4
- data/LICENSE.txt +1 -1
- data/README.rdoc +3 -3
- data/lib/loaded_die.rb +46 -1
- data/loaded_die.gemspec +2 -2
- data/test/loaded_die_test.rb +3 -5
- metadata +6 -7
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b04718fe3728aa1a6f354e81eed37708842a79fb62b6151f6d9a2823bccd603e
|
|
4
|
+
data.tar.gz: a6993994255411fdcd889aab140e8c8a1cb8453fad9ad44fb972076330409534
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 52d5caf5851d300a185600d5a6e00203bebf672913a78ab42ca4430a369b5d690033fd679ccbef1df8abac7bbdd6c63e77d8f04067f4445142caf4b052e0850c
|
|
7
|
+
data.tar.gz: 2d896e03013c765bfe6beec383ffa456e9e4c199573d8b77900d3a7badef92f70d0a90a49e467317b98308b6779246c34e52401aec1d602b7e59e844071ab94b
|
data/LICENSE.txt
CHANGED
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
|
|
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
|
-
|
|
37
|
-
random number generator will be sent
|
|
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
|
|
11
|
-
s.description = %q{Loaded Die makes it easy to choose randomly
|
|
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"
|
data/test/loaded_die_test.rb
CHANGED
|
@@ -27,11 +27,9 @@ lambda do
|
|
|
27
27
|
sampler.respond_to?(:sample) or fail
|
|
28
28
|
end.call
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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.
|
|
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:
|
|
11
|
+
date: 2022-12-01 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
|
-
description: Loaded Die makes it easy to choose randomly
|
|
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
|
-
|
|
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
|
|
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
|