Module: RDGC::Util::RandomUtil
- Defined in:
- lib/rdgc/util/random_util.rb
Class Method Summary collapse
- .bool_rand ⇒ Object
- .dice(n, max) ⇒ Object
- .range_rand(s_val, e_val) ⇒ Object
- .select_rand(hash) ⇒ Object
Class Method Details
.bool_rand ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/rdgc/util/random_util.rb', line 8 def bool_rand case rand(2) when 1 true when 0 false end end |
.dice(n, max) ⇒ Object
48 49 50 51 52 |
# File 'lib/rdgc/util/random_util.rb', line 48 def dice(n, max) ret = 0 n.times{ret += range_rand(1, max)} ret end |
.range_rand(s_val, e_val) ⇒ Object
17 18 19 20 |
# File 'lib/rdgc/util/random_util.rb', line 17 def range_rand(s_val, e_val) return s_val if e_val <= s_val s_val + rand((e_val - s_val)+1) end |
.select_rand(hash) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/rdgc/util/random_util.rb', line 22 def select_rand(hash) return unless hash return if hash.empty? range_list = [] count = 0 hash.each do |k, v| range = count...(count + v) range_list << [range, k] count += v end return if count <= 0 val = rand(count) ret = nil range_list.each do |r| if r.first.include?(val) ret = r.last break end end ret end |