hazard 1.3.1 → 1.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -1
- data/README.md +1 -1
- data/hazard.gemspec +1 -1
- data/lib/hazard/version.rb +1 -1
- data/lib/weighted_table.rb +19 -19
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8ad65f2c9f179677ed7f59455fb1836af756e5d1
|
4
|
+
data.tar.gz: 2f8d5c6da1b99735448fe3ab51f5bb544f9cc6ce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a6cb6d3620502079175076aab7757136eb872be30da81357bf84f2ad268180dbc38b441e86059562de36ff1a34f7782dd056e2ba50edc7fe175496806a4ebe18
|
7
|
+
data.tar.gz: 1be600fddf5135d8223317612eb7f03388e683c9195911be18d48b298ff9c607de1ae7952163dafcff3317fbbe57929618664c4133a335208003d5efc6328be8
|
data/Gemfile
CHANGED
data/README.md
CHANGED
data/hazard.gemspec
CHANGED
@@ -25,7 +25,7 @@ Gem::Specification.new do |spec|
|
|
25
25
|
spec.require_paths = ['lib']
|
26
26
|
|
27
27
|
# spec.add_development_dependency 'bundler', '~> 1.13'
|
28
|
-
spec.add_development_dependency 'rake', '~>
|
28
|
+
spec.add_development_dependency 'rake', '~> 13.0'
|
29
29
|
spec.add_development_dependency 'minitest', '~> 5.0'
|
30
30
|
|
31
31
|
spec.required_ruby_version = '>= 2.0'
|
data/lib/hazard/version.rb
CHANGED
data/lib/weighted_table.rb
CHANGED
@@ -7,12 +7,12 @@ class WeightedTable
|
|
7
7
|
|
8
8
|
BASE_WEIGHT = 0
|
9
9
|
|
10
|
-
|
11
|
-
|
10
|
+
# Initialize a WeightedTable
|
11
|
+
# setting floating_points to true indicate that you will work with floating points rather than integers.
|
12
12
|
def initialize( floating_points: false )
|
13
13
|
@weights = []
|
14
14
|
@max_weight = BASE_WEIGHT
|
15
|
-
|
15
|
+
@floating_points = floating_points
|
16
16
|
end
|
17
17
|
|
18
18
|
# Load a WeightedTable with data
|
@@ -32,21 +32,21 @@ class WeightedTable
|
|
32
32
|
def from_weighted_table( table )
|
33
33
|
raise 'Table must contain at least one element' if table.empty?
|
34
34
|
|
35
|
-
|
36
|
-
|
35
|
+
# We may call this method many time for an existing object, so we must clear it
|
36
|
+
@weights.clear
|
37
37
|
|
38
38
|
base = BASE_WEIGHT
|
39
|
-
|
39
|
+
w = nil
|
40
40
|
|
41
41
|
table.each do |weight, data|
|
42
42
|
w = base + weight
|
43
|
-
|
43
|
+
@weights << [base, w, data]
|
44
44
|
base = w
|
45
|
-
|
45
|
+
end
|
46
46
|
|
47
|
-
|
47
|
+
@max_weight = w
|
48
48
|
|
49
|
-
|
49
|
+
# p @weights, @max_weight
|
50
50
|
|
51
51
|
self
|
52
52
|
end
|
@@ -65,18 +65,18 @@ class WeightedTable
|
|
65
65
|
# @return [Object] a random object given at the building of the table.
|
66
66
|
def sample
|
67
67
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
68
|
+
# ... means that @max_weight is excluded
|
69
|
+
if @floating_points
|
70
|
+
r = Kernel.rand( 0.0...@max_weight.to_f )
|
71
|
+
else
|
72
|
+
r = Kernel.rand( 0...@max_weight )
|
73
|
+
end
|
74
74
|
|
75
|
-
|
75
|
+
# p r
|
76
76
|
|
77
77
|
@weights.each do |base_weight, max_weight, data|
|
78
|
-
|
79
|
-
|
78
|
+
return data if r >= base_weight && r < max_weight
|
79
|
+
end
|
80
80
|
|
81
81
|
raise 'Rand not in key range'
|
82
82
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hazard
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.3.
|
4
|
+
version: 1.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cédric ZUGER
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-03-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '13.0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '13.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: minitest
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|