pick_me_too 1.1.2 → 2.0.0
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/.github/workflows/ci.yml +24 -0
- data/.gitignore +1 -1
- data/.ruby-version +1 -0
- data/CHANGES.md +6 -0
- data/Gemfile +5 -0
- data/README.md +6 -6
- data/Rakefile +2 -2
- data/lib/pick_me_too.rb +47 -83
- data/pick_me_too.gemspec +11 -9
- data/test/basic_test.rb +68 -4
- data/test/documentation_test.rb +3 -4
- metadata +21 -60
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 510b87a471ae9ebfef4f89f164182c05e2d3a0c3270c1e730ffc27c95ba00646
|
|
4
|
+
data.tar.gz: fe4f0efd9240c02d1e6fc4d967eeda9d55f786b8d864a932d5dc3bee3fcffc86
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d581207db172f9525305f0f8ccac30f53edfcdb7c12e5578f37195a480c50e983820baf25cf69741e71c4f8d7e102da651882d01297475964aace6812972443e
|
|
7
|
+
data.tar.gz: 42f1dfacdbf6c002643b778c9f97693f9e30ae4c3569ac4bc96fac6c6e43ce05ff3b9b8a8e5d1c901f2c0ab33880ccd6c4cf42b8ebd790f50017dd1517110211
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
test:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
strategy:
|
|
12
|
+
fail-fast: false
|
|
13
|
+
matrix:
|
|
14
|
+
ruby-version: ['3.1', '3.2', '3.3', '3.4', '4.0', 'head']
|
|
15
|
+
steps:
|
|
16
|
+
- uses: actions/checkout@v4
|
|
17
|
+
- uses: ruby/setup-ruby@v1
|
|
18
|
+
with:
|
|
19
|
+
ruby-version: ${{ matrix.ruby-version }}
|
|
20
|
+
bundler: latest
|
|
21
|
+
- name: Install dependencies
|
|
22
|
+
run: bundle install
|
|
23
|
+
- name: Run tests
|
|
24
|
+
run: bundle exec rake
|
data/.gitignore
CHANGED
data/.ruby-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
3.4.1
|
data/CHANGES.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
+
## 2.0.0 *2026-11-28*
|
|
4
|
+
* replaced the `eval`-compiled tree of comparisons with [Vose's alias method](https://en.wikipedia.org/wiki/Alias_method): urn setup is O(n), every pick is O(1), and no code is generated with `eval` at runtime (brakeman no longer reports Dangerous Eval). ***NOTE***: as with 1.1.1, this is not considered a breaking API change, though it does change the sequence of items picked by a fixed random number sequence
|
|
5
|
+
* dropped support for Ruby < 3.1 (`required_ruby_version` is now `>= 3.1`)
|
|
6
|
+
* modernized development dependencies: minitest `>= 5.20, < 7`, rake `~> 13.0`; removed the unused `json` and `byebug` dependencies and the ancient `bundler ~> 1.7` pin
|
|
7
|
+
* added a `Gemfile`, gemspec `metadata`, and a GitHub Actions CI matrix (Ruby 3.1 through head)
|
|
8
|
+
* `.ruby-version` is now tracked in git
|
|
3
9
|
## 1.1.2 *2022-11-6*
|
|
4
10
|
* optimized ternerization code still further
|
|
5
11
|
## 1.1.1 *2022-10-30*
|
data/Gemfile
ADDED
data/README.md
CHANGED
|
@@ -18,7 +18,7 @@ picker = PickMeToo.new([["prevention", 1], ["cure", 16]], -> { rng.rand })
|
|
|
18
18
|
counter = Hash.new 0
|
|
19
19
|
32.times { counter[picker.pick] += 1 }
|
|
20
20
|
counter
|
|
21
|
-
# => {"cure"=>
|
|
21
|
+
# => {"cure"=>29, "prevention"=>3}
|
|
22
22
|
|
|
23
23
|
# you can also use a hash to map items to frequencies
|
|
24
24
|
# frequencies don't need to be whole numbers
|
|
@@ -29,10 +29,10 @@ picker = PickMeToo.new({foo: 1, bar: 2, baz: 0.5}, -> { rng.rand })
|
|
|
29
29
|
counter = Hash.new 0
|
|
30
30
|
32.times { counter[picker.pick] += 1 }
|
|
31
31
|
counter
|
|
32
|
-
# => {:bar=>
|
|
32
|
+
# => {:bar=>15, :foo=>12, :baz=>5}
|
|
33
33
|
|
|
34
34
|
# you don't need to provide your own random number sequence
|
|
35
|
-
picker = PickMeToo({a: 1, b: 2, c: 3})
|
|
35
|
+
picker = PickMeToo.new({a: 1, b: 2, c: 3})
|
|
36
36
|
# ...
|
|
37
37
|
```
|
|
38
38
|
|
|
@@ -57,9 +57,9 @@ and then pick randomly from the array:
|
|
|
57
57
|
|
|
58
58
|
This is inefficient or impossible if the frequencies are huge or excessively precise. Say you want pi orcs in your list and e goblins.
|
|
59
59
|
|
|
60
|
-
`PickMeToo` requires just one item of each type. It converts the frequencies to probabilities and
|
|
61
|
-
|
|
62
|
-
|
|
60
|
+
`PickMeToo` requires just one item of each type. It converts the frequencies to probabilities and uses
|
|
61
|
+
[Vose's alias method](https://en.wikipedia.org/wiki/Alias_method): setting up the urn takes O(n) time and each
|
|
62
|
+
pick takes O(1) time, no matter how many items the urn contains or how skewed the frequencies are.
|
|
63
63
|
|
|
64
64
|
|
|
65
65
|
# API
|
data/Rakefile
CHANGED
data/lib/pick_me_too.rb
CHANGED
|
@@ -15,11 +15,17 @@
|
|
|
15
15
|
#
|
|
16
16
|
# Items once picked are "placed back in the urn", so if you pick a cat this doesn't reduce the
|
|
17
17
|
# probability the next thing you pick is also a cat, and the urn will never be picked empty.
|
|
18
|
+
#
|
|
19
|
+
# Sampling uses Vose's alias method: the urn is set up in O(n) time and each pick is O(1),
|
|
20
|
+
# regardless of how many items the urn contains.
|
|
18
21
|
class PickMeToo
|
|
19
|
-
VERSION = '
|
|
22
|
+
VERSION = '2.0.0'
|
|
20
23
|
|
|
21
24
|
class Error < StandardError; end
|
|
22
25
|
|
|
26
|
+
# The default source of randomness: a single call to Kernel#rand per pick.
|
|
27
|
+
DEFAULT_RNG = -> { rand }
|
|
28
|
+
|
|
23
29
|
##
|
|
24
30
|
# "Fill" the urn.
|
|
25
31
|
#
|
|
@@ -34,23 +40,22 @@ class PickMeToo
|
|
|
34
40
|
# - there are no pairs in the frequency list
|
|
35
41
|
# - any of the frequencies is non-positive
|
|
36
42
|
# - any of the items in the list isn't something followed by a number
|
|
37
|
-
def initialize(frequencies, rnd =
|
|
43
|
+
def initialize(frequencies, rnd = DEFAULT_RNG)
|
|
38
44
|
@rnd = rnd
|
|
39
|
-
|
|
40
|
-
@objects =
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
else
|
|
44
|
-
root = optimize(frequencies)
|
|
45
|
-
# compile everything into a nested ternary expression
|
|
46
|
-
@picker = eval "->(p) { #{ternarize(root)} }"
|
|
47
|
-
end
|
|
45
|
+
pairs = validate(Array(frequencies))
|
|
46
|
+
@objects = pairs.map(&:first)
|
|
47
|
+
@size = pairs.length
|
|
48
|
+
build_tables(pairs.map { |(_, weight)| weight.to_f })
|
|
48
49
|
end
|
|
49
50
|
|
|
50
51
|
##
|
|
51
52
|
# Pick an item from the urn.
|
|
52
53
|
def pick
|
|
53
|
-
@
|
|
54
|
+
scaled = @rnd.call * @size
|
|
55
|
+
i = scaled.to_i
|
|
56
|
+
i = 0 if i.negative?
|
|
57
|
+
i = @size - 1 if i >= @size
|
|
58
|
+
(scaled - i) < @prob[i] ? @objects[i] : @objects[@alias[i]]
|
|
54
59
|
end
|
|
55
60
|
|
|
56
61
|
##
|
|
@@ -62,91 +67,50 @@ class PickMeToo
|
|
|
62
67
|
#
|
|
63
68
|
# This is useful if you want to switch from a seeded random number generator
|
|
64
69
|
# to something more truly random.
|
|
65
|
-
def randomize!(rnd =
|
|
70
|
+
def randomize!(rnd = DEFAULT_RNG)
|
|
66
71
|
@rnd = rnd
|
|
67
72
|
end
|
|
68
73
|
|
|
69
74
|
private
|
|
70
75
|
|
|
71
|
-
# sanity check
|
|
72
|
-
def
|
|
73
|
-
raise Error, 'no frequencies given'
|
|
74
|
-
|
|
76
|
+
# sanity check of the frequencies
|
|
77
|
+
def validate(frequencies)
|
|
78
|
+
raise Error, 'no frequencies given' if frequencies.empty?
|
|
79
|
+
|
|
80
|
+
unless frequencies.all? { |pair| pair.is_a?(Array) && pair.length == 2 && pair[1].is_a?(Numeric) }
|
|
75
81
|
raise Error, 'all frequencies must be two-member arrays the second member of which is Numeric'
|
|
76
82
|
end
|
|
77
83
|
|
|
78
|
-
|
|
84
|
+
bad = frequencies.reject { |(_, weight)| weight.positive? }
|
|
79
85
|
raise Error, "the following have non-positive frequencies: #{bad.inspect}" if bad.any?
|
|
80
86
|
|
|
81
|
-
|
|
82
|
-
# sort by size of probability interval
|
|
83
|
-
# in general we will want to consider wide intervals before narrow ones
|
|
84
|
-
good.sort_by(&:last).reverse.map { |o, n| [o, n / total] }
|
|
85
|
-
end
|
|
86
|
-
|
|
87
|
-
# optimize the order of threshold comparisons to map a random number to an index in the array
|
|
88
|
-
# of choices
|
|
89
|
-
def optimize(frequencies)
|
|
90
|
-
frequencies = frequencies.each_with_index.map { |(*, i), idx| { interval: i, index: idx } }
|
|
91
|
-
root = build_branch(frequencies)
|
|
92
|
-
add_thresholds(root, 0)
|
|
93
|
-
root
|
|
94
|
-
end
|
|
95
|
-
|
|
96
|
-
def add_thresholds(node, acc)
|
|
97
|
-
# acc represents the accumulated probability mass known to be before anything in the tree
|
|
98
|
-
# currently under consideration
|
|
99
|
-
if (l = node[:left])
|
|
100
|
-
add_thresholds(l, acc)
|
|
101
|
-
node[:left_threshold] = acc += l[:sum]
|
|
102
|
-
end
|
|
103
|
-
if (r = node[:right])
|
|
104
|
-
acc = node[:right_threshold] = acc + node[:interval]
|
|
105
|
-
add_thresholds(r, acc)
|
|
106
|
-
end
|
|
87
|
+
frequencies
|
|
107
88
|
end
|
|
108
89
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
# build a binary-branching search tree
|
|
118
|
-
left, right = frequencies.each_with_index.partition { |*, i| left_branch?(i + 1) }
|
|
119
|
-
node[:left] = build_branch(left.map(&:first))
|
|
120
|
-
node[:right] = build_branch(right.map(&:first)) if right.any?
|
|
121
|
-
end
|
|
122
|
-
end
|
|
123
|
-
node[:sum] = sum
|
|
124
|
-
node
|
|
125
|
-
end
|
|
90
|
+
# Vose's alias method: precompute, for each of the n slots, a probability and an alias,
|
|
91
|
+
# so that a single random number in [0, 1) maps to an item in constant time.
|
|
92
|
+
def build_tables(weights)
|
|
93
|
+
n = weights.length
|
|
94
|
+
# normalize by the largest weight so even astronomically large frequencies cannot overflow
|
|
95
|
+
max = weights.max
|
|
96
|
+
total = weights.sum { |w| w / max }
|
|
97
|
+
scaled = weights.map { |w| w / max * n / total }
|
|
126
98
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
elsif index < 1
|
|
133
|
-
false
|
|
134
|
-
else
|
|
135
|
-
left_branch?((index - 1) / 2)
|
|
136
|
-
end
|
|
137
|
-
end
|
|
99
|
+
@prob = Array.new(n)
|
|
100
|
+
@alias = Array.new(n, 0)
|
|
101
|
+
small = []
|
|
102
|
+
large = []
|
|
103
|
+
scaled.each_with_index { |s, i| (s < 1.0 ? small : large) << i }
|
|
138
104
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
elsif r
|
|
147
|
-
"(p > #{node[:right_threshold]} ? #{ternarize(r)} : #{node[:index]})"
|
|
148
|
-
else
|
|
149
|
-
node[:index]
|
|
105
|
+
until small.empty? || large.empty?
|
|
106
|
+
s = small.pop
|
|
107
|
+
l = large.pop
|
|
108
|
+
@prob[s] = scaled[s]
|
|
109
|
+
@alias[s] = l
|
|
110
|
+
scaled[l] += scaled[s] - 1.0
|
|
111
|
+
(scaled[l] < 1.0 ? small : large) << l
|
|
150
112
|
end
|
|
113
|
+
# anything left over (float rounding) gets probability 1
|
|
114
|
+
(small + large).each { |i| @prob[i] = 1.0 }
|
|
151
115
|
end
|
|
152
116
|
end
|
data/pick_me_too.gemspec
CHANGED
|
@@ -16,17 +16,19 @@ Gem::Specification.new do |s|
|
|
|
16
16
|
DESC
|
|
17
17
|
s.authors = ['David F. Houghton']
|
|
18
18
|
s.email = 'dfhoughton@gmail.com'
|
|
19
|
-
s.homepage =
|
|
20
|
-
|
|
21
|
-
s.
|
|
22
|
-
s.required_ruby_version = '>= 2.5.3'
|
|
19
|
+
s.homepage = 'https://github.com/dfhoughton/pick_me_too'
|
|
20
|
+
s.license = 'MIT'
|
|
21
|
+
s.required_ruby_version = '>= 3.1'
|
|
23
22
|
s.files = `git ls-files -z`.split("\x0")
|
|
24
23
|
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
|
25
24
|
s.require_paths = ['lib']
|
|
26
25
|
|
|
27
|
-
s.
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
26
|
+
s.metadata = {
|
|
27
|
+
'bug_tracker_uri' => 'https://github.com/dfhoughton/pick_me_too/issues',
|
|
28
|
+
'changelog_uri' => 'https://github.com/dfhoughton/pick_me_too/blob/main/CHANGES.md',
|
|
29
|
+
'source_code_uri' => 'https://github.com/dfhoughton/pick_me_too'
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
s.add_development_dependency 'minitest', '>= 5.20', '< 7'
|
|
33
|
+
s.add_development_dependency 'rake', '~> 13.0'
|
|
32
34
|
end
|
data/test/basic_test.rb
CHANGED
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
require 'minitest/autorun'
|
|
4
4
|
|
|
5
5
|
require 'pick_me_too'
|
|
6
|
-
require 'byebug'
|
|
7
6
|
|
|
8
7
|
# :stopdoc:
|
|
9
8
|
|
|
@@ -31,7 +30,7 @@ class BasicTest < Minitest::Test
|
|
|
31
30
|
frequencies = [['cat', 1], ['dog', 2], ['horse', 3], ['camel', 4], ['lizard', 5], ['fish', 6]]
|
|
32
31
|
picker = PickMeToo.new(frequencies, -> { rnd.rand })
|
|
33
32
|
counter = Hash.new(0)
|
|
34
|
-
(frequencies.
|
|
33
|
+
(frequencies.sum { |(_, n)| n } * 1000).times { counter[picker.pick] += 1 }
|
|
35
34
|
frequencies.each do |key, n|
|
|
36
35
|
assert_equal n, (counter[key] / 1000.0).round, "right number of #{key}"
|
|
37
36
|
end
|
|
@@ -57,6 +56,12 @@ class BasicTest < Minitest::Test
|
|
|
57
56
|
end
|
|
58
57
|
end
|
|
59
58
|
|
|
59
|
+
def test_zero_frequency
|
|
60
|
+
assert_raises(PickMeToo::Error) do
|
|
61
|
+
PickMeToo.new([['foo', 1], ['bar', 0]])
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
60
65
|
def test_all_tuples
|
|
61
66
|
assert_raises(PickMeToo::Error,
|
|
62
67
|
'all frequencies must be two-member arrays the second member of which is Numeric') do
|
|
@@ -71,13 +76,19 @@ class BasicTest < Minitest::Test
|
|
|
71
76
|
end
|
|
72
77
|
end
|
|
73
78
|
|
|
79
|
+
def test_non_numeric_frequency
|
|
80
|
+
assert_raises(PickMeToo::Error) do
|
|
81
|
+
PickMeToo.new([['foo', 'a lot']])
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
74
85
|
# force unary branching
|
|
75
86
|
def test_big
|
|
76
87
|
rnd = Random.new 1
|
|
77
88
|
frequencies = %w[a b c d e f g h].each_with_index.map { |k, i| [k, 2**i] }
|
|
78
89
|
picker = PickMeToo.new(frequencies, -> { rnd.rand })
|
|
79
90
|
counter = Hash.new(0)
|
|
80
|
-
(frequencies.
|
|
91
|
+
(frequencies.sum { |(_, n)| n } * 6000).times { counter[picker.pick] += 1 }
|
|
81
92
|
frequencies.each do |key, n|
|
|
82
93
|
assert_equal n, (counter[key] / 6000.0).round, "right number of #{key}"
|
|
83
94
|
end
|
|
@@ -89,12 +100,28 @@ class BasicTest < Minitest::Test
|
|
|
89
100
|
frequencies = %w[a b c d e f g h].each_with_index.map { |k, _i| [k, 1] }
|
|
90
101
|
picker = PickMeToo.new(frequencies, -> { rnd.rand })
|
|
91
102
|
counter = Hash.new(0)
|
|
92
|
-
(frequencies.
|
|
103
|
+
(frequencies.sum { |(_, n)| n } * 6000).times { counter[picker.pick] += 1 }
|
|
93
104
|
frequencies.each do |key, n|
|
|
94
105
|
assert_equal n, (counter[key] / 6000.0).round, "right number of #{key}"
|
|
95
106
|
end
|
|
96
107
|
end
|
|
97
108
|
|
|
109
|
+
# many items: picks stay O(1) and the distribution stays right
|
|
110
|
+
def test_many_items
|
|
111
|
+
rnd = Random.new 1
|
|
112
|
+
frequencies = (1..1000).map { |i| ["item#{i}", i] }
|
|
113
|
+
picker = PickMeToo.new(frequencies, -> { rnd.rand })
|
|
114
|
+
counter = Hash.new(0)
|
|
115
|
+
100_000.times { counter[picker.pick] += 1 }
|
|
116
|
+
total = frequencies.sum { |(_, n)| n }.to_f
|
|
117
|
+
frequencies.each do |key, n|
|
|
118
|
+
expected = n / total * 100_000
|
|
119
|
+
# 4-sigma band, with a floor so that very rare items don't false-fail
|
|
120
|
+
delta = 4 * Math.sqrt(expected) + 5
|
|
121
|
+
assert_in_delta expected, counter[key], delta, "right number of #{key}"
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
98
125
|
def test_randomize
|
|
99
126
|
rnd1 = Random.new 1
|
|
100
127
|
rnd2 = Random.new 1
|
|
@@ -109,4 +136,41 @@ class BasicTest < Minitest::Test
|
|
|
109
136
|
ar2 = Array.new(100) { picker2.pick }
|
|
110
137
|
refute_equal ar1, ar2, 'if we randomize a picker, we get a new sequence'
|
|
111
138
|
end
|
|
139
|
+
|
|
140
|
+
def test_randomize_with_no_argument
|
|
141
|
+
picker = PickMeToo.new({ foo: 1, bar: 2 }, -> { 0.5 })
|
|
142
|
+
picker.randomize!
|
|
143
|
+
10.times { assert_includes [:foo, :bar], picker.pick }
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
# an rnd that returns exactly 1.0 must not blow up
|
|
147
|
+
def test_rnd_at_one
|
|
148
|
+
picker = PickMeToo.new({ a: 1, b: 1 }, -> { 1.0 })
|
|
149
|
+
assert_includes [:a, :b], picker.pick
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def test_rnd_at_zero
|
|
153
|
+
picker = PickMeToo.new({ a: 1, b: 1 }, -> { 0.0 })
|
|
154
|
+
assert_equal :a, picker.pick
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
# astronomically large frequencies must not overflow
|
|
158
|
+
def test_huge_frequencies
|
|
159
|
+
rnd = Random.new 1
|
|
160
|
+
picker = PickMeToo.new({ a: 1e300, b: 1e300, c: 1 }, -> { rnd.rand })
|
|
161
|
+
counter = Hash.new(0)
|
|
162
|
+
3000.times { counter[picker.pick] += 1 }
|
|
163
|
+
assert_equal 0, counter[:c], 'c is effectively never picked'
|
|
164
|
+
assert counter[:a] + counter[:b] == 3000
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
# fractional and irrational frequencies
|
|
168
|
+
def test_irrational_frequencies
|
|
169
|
+
rnd = Random.new 1
|
|
170
|
+
picker = PickMeToo.new({ e: Math::E, pi: Math::PI }, -> { rnd.rand })
|
|
171
|
+
counter = Hash.new(0)
|
|
172
|
+
6000.times { counter[picker.pick] += 1 }
|
|
173
|
+
assert_in_delta 6000 * Math::E / (Math::E + Math::PI), counter[:e], 200
|
|
174
|
+
assert_in_delta 6000 * Math::PI / (Math::E + Math::PI), counter[:pi], 200
|
|
175
|
+
end
|
|
112
176
|
end
|
data/test/documentation_test.rb
CHANGED
|
@@ -3,17 +3,16 @@
|
|
|
3
3
|
require 'minitest/autorun'
|
|
4
4
|
|
|
5
5
|
require 'pick_me_too'
|
|
6
|
-
require 'byebug'
|
|
7
6
|
|
|
8
7
|
# :stopdoc:
|
|
9
8
|
|
|
10
|
-
class
|
|
9
|
+
class DocumentationTest < Minitest::Test
|
|
11
10
|
def test_synopsis
|
|
12
11
|
rng = Random.new 1
|
|
13
12
|
picker = PickMeToo.new([['prevention', 1], ['cure', 16]], -> { rng.rand })
|
|
14
13
|
counter = Hash.new 0
|
|
15
14
|
32.times { counter[picker.pick] += 1 }
|
|
16
|
-
assert_equal({ 'cure' =>
|
|
15
|
+
assert_equal({ 'cure' => 29, 'prevention' => 3 }, counter)
|
|
17
16
|
end
|
|
18
17
|
|
|
19
18
|
def test_synopsis_hash
|
|
@@ -21,6 +20,6 @@ class BasicTest < Minitest::Test
|
|
|
21
20
|
picker = PickMeToo.new({ foo: 1, bar: 2, baz: 0.5 }, -> { rng.rand })
|
|
22
21
|
counter = Hash.new 0
|
|
23
22
|
32.times { counter[picker.pick] += 1 }
|
|
24
|
-
assert_equal({ bar:
|
|
23
|
+
assert_equal({ bar: 15, foo: 12, baz: 5 }, counter)
|
|
25
24
|
end
|
|
26
25
|
end
|
metadata
CHANGED
|
@@ -1,91 +1,48 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: pick_me_too
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 2.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- David F. Houghton
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 2026-11-28 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
|
-
name:
|
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
|
16
|
-
requirements:
|
|
17
|
-
- - "~>"
|
|
18
|
-
- !ruby/object:Gem::Version
|
|
19
|
-
version: '1.7'
|
|
20
|
-
type: :development
|
|
21
|
-
prerelease: false
|
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
-
requirements:
|
|
24
|
-
- - "~>"
|
|
25
|
-
- !ruby/object:Gem::Version
|
|
26
|
-
version: '1.7'
|
|
27
|
-
- !ruby/object:Gem::Dependency
|
|
28
|
-
name: byebug
|
|
13
|
+
name: minitest
|
|
29
14
|
requirement: !ruby/object:Gem::Requirement
|
|
30
15
|
requirements:
|
|
31
|
-
- - "~>"
|
|
32
|
-
- !ruby/object:Gem::Version
|
|
33
|
-
version: '9.1'
|
|
34
16
|
- - ">="
|
|
35
17
|
- !ruby/object:Gem::Version
|
|
36
|
-
version:
|
|
37
|
-
|
|
38
|
-
prerelease: false
|
|
39
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
40
|
-
requirements:
|
|
41
|
-
- - "~>"
|
|
18
|
+
version: '5.20'
|
|
19
|
+
- - "<"
|
|
42
20
|
- !ruby/object:Gem::Version
|
|
43
|
-
version: '
|
|
44
|
-
- - ">="
|
|
45
|
-
- !ruby/object:Gem::Version
|
|
46
|
-
version: 9.1.0
|
|
47
|
-
- !ruby/object:Gem::Dependency
|
|
48
|
-
name: json
|
|
49
|
-
requirement: !ruby/object:Gem::Requirement
|
|
50
|
-
requirements:
|
|
51
|
-
- - "~>"
|
|
52
|
-
- !ruby/object:Gem::Version
|
|
53
|
-
version: '2'
|
|
21
|
+
version: '7'
|
|
54
22
|
type: :development
|
|
55
23
|
prerelease: false
|
|
56
24
|
version_requirements: !ruby/object:Gem::Requirement
|
|
57
25
|
requirements:
|
|
58
|
-
- - "
|
|
59
|
-
- !ruby/object:Gem::Version
|
|
60
|
-
version: '2'
|
|
61
|
-
- !ruby/object:Gem::Dependency
|
|
62
|
-
name: minitest
|
|
63
|
-
requirement: !ruby/object:Gem::Requirement
|
|
64
|
-
requirements:
|
|
65
|
-
- - "~>"
|
|
26
|
+
- - ">="
|
|
66
27
|
- !ruby/object:Gem::Version
|
|
67
|
-
version: '5'
|
|
68
|
-
|
|
69
|
-
prerelease: false
|
|
70
|
-
version_requirements: !ruby/object:Gem::Requirement
|
|
71
|
-
requirements:
|
|
72
|
-
- - "~>"
|
|
28
|
+
version: '5.20'
|
|
29
|
+
- - "<"
|
|
73
30
|
- !ruby/object:Gem::Version
|
|
74
|
-
version: '
|
|
31
|
+
version: '7'
|
|
75
32
|
- !ruby/object:Gem::Dependency
|
|
76
33
|
name: rake
|
|
77
34
|
requirement: !ruby/object:Gem::Requirement
|
|
78
35
|
requirements:
|
|
79
36
|
- - "~>"
|
|
80
37
|
- !ruby/object:Gem::Version
|
|
81
|
-
version: '
|
|
38
|
+
version: '13.0'
|
|
82
39
|
type: :development
|
|
83
40
|
prerelease: false
|
|
84
41
|
version_requirements: !ruby/object:Gem::Requirement
|
|
85
42
|
requirements:
|
|
86
43
|
- - "~>"
|
|
87
44
|
- !ruby/object:Gem::Version
|
|
88
|
-
version: '
|
|
45
|
+
version: '13.0'
|
|
89
46
|
description: PickMeToo is a Ruby urn model. It allows you to generate an "urn" from
|
|
90
47
|
which you can randomly sample items with specified frequencies. This facilitates
|
|
91
48
|
modeling things that occur with known frequencies, like weather or wandering monsters.
|
|
@@ -94,8 +51,11 @@ executables: []
|
|
|
94
51
|
extensions: []
|
|
95
52
|
extra_rdoc_files: []
|
|
96
53
|
files:
|
|
54
|
+
- ".github/workflows/ci.yml"
|
|
97
55
|
- ".gitignore"
|
|
56
|
+
- ".ruby-version"
|
|
98
57
|
- CHANGES.md
|
|
58
|
+
- Gemfile
|
|
99
59
|
- LICENSE
|
|
100
60
|
- README.md
|
|
101
61
|
- Rakefile
|
|
@@ -106,8 +66,10 @@ files:
|
|
|
106
66
|
homepage: https://github.com/dfhoughton/pick_me_too
|
|
107
67
|
licenses:
|
|
108
68
|
- MIT
|
|
109
|
-
metadata:
|
|
110
|
-
|
|
69
|
+
metadata:
|
|
70
|
+
bug_tracker_uri: https://github.com/dfhoughton/pick_me_too/issues
|
|
71
|
+
changelog_uri: https://github.com/dfhoughton/pick_me_too/blob/main/CHANGES.md
|
|
72
|
+
source_code_uri: https://github.com/dfhoughton/pick_me_too
|
|
111
73
|
rdoc_options: []
|
|
112
74
|
require_paths:
|
|
113
75
|
- lib
|
|
@@ -115,15 +77,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
115
77
|
requirements:
|
|
116
78
|
- - ">="
|
|
117
79
|
- !ruby/object:Gem::Version
|
|
118
|
-
version:
|
|
80
|
+
version: '3.1'
|
|
119
81
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
120
82
|
requirements:
|
|
121
83
|
- - ">="
|
|
122
84
|
- !ruby/object:Gem::Version
|
|
123
85
|
version: '0'
|
|
124
86
|
requirements: []
|
|
125
|
-
rubygems_version: 3.
|
|
126
|
-
signing_key:
|
|
87
|
+
rubygems_version: 3.6.2
|
|
127
88
|
specification_version: 4
|
|
128
89
|
summary: Randomly select items from a list with specified frequencies
|
|
129
90
|
test_files:
|