weighted_random_selector 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/weighted_random_selector.rb +23 -0
- metadata +46 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c17875b45bf0470dd32ffefb016f19bbb34ac0580af07e7800a7e9186e768c5a
|
4
|
+
data.tar.gz: 9b8c21f04870070602804eae5603635162a7b2074064db3cfff5bd32269ef3b9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6b1432a4217bf87189bbf82ae7b27cdeb4a2b345dd9fd0a0b94bdea875171c44b081c4d71329b0f19c0dd153bf170b33f50a1202d9909ca1a262736ade69f274
|
7
|
+
data.tar.gz: 387c1ad9b156e4e9ffe5e09410d9c398f2803fb9f8653310cb8119c6d5c1500aaa1c037169a41933e87af29a43b80cc883790d8f85a8e07674858500a98bf543
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class WeightedRandomSelector
|
2
|
+
|
3
|
+
def self.select_from_hash_list(hash_list)
|
4
|
+
|
5
|
+
# Calculate the sum of the weights
|
6
|
+
weighted_sum = hash_list.map{|k,v| v}.reduce(:+)
|
7
|
+
random_num = 1 + rand(weighted_sum)
|
8
|
+
|
9
|
+
# Imagine random_num is a number between 1 and the sum of the weights.
|
10
|
+
# This range is partitioned to identify each hash key.
|
11
|
+
|
12
|
+
# Ex: If the key A has a weight of 4 and B has a weight of 10
|
13
|
+
# Weighted sum will be 14. If the random number is 1-4 then A will be selected
|
14
|
+
# If the random number is 5-14 then B will be selected
|
15
|
+
|
16
|
+
counter = 0
|
17
|
+
hash_list.each do |k,v|
|
18
|
+
counter += v
|
19
|
+
return k if counter >= random_num
|
20
|
+
end
|
21
|
+
raise "Something went wrong with this gem."
|
22
|
+
end
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: weighted_random_selector
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nicholas Ellul
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-03-27 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: When the method `select_from_hash_list` is given an array of hashes where
|
14
|
+
the value of each hash is a weight, the gem will take that into account and select
|
15
|
+
a value at random.
|
16
|
+
email: nicholas.ellul1@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/weighted_random_selector.rb
|
22
|
+
homepage: http://rubygems.org/gems/weighted_random_selector
|
23
|
+
licenses:
|
24
|
+
- MIT
|
25
|
+
metadata: {}
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options: []
|
28
|
+
require_paths:
|
29
|
+
- lib
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 2.7.6
|
43
|
+
signing_key:
|
44
|
+
specification_version: 4
|
45
|
+
summary: Randomly select value based on weights
|
46
|
+
test_files: []
|