class Nametrainer::RNG
A random number generator.
Get a random index from 0 to (size - 1), where smaller indices are preferred, with
rng = RNG.new(:size => size, :weighting_factor => 6) rng.rand
Index 0 will be :weighting_factor times more probable than the highest index.
Public Class Methods
new(args)
click to toggle source
# File lib/nametrainer/rng.rb, line 15 def initialize(args) @size = args[:size] @weighting_factor = args[:weighting_factor] @limits = normalized_limits end
Public Instance Methods
rand()
click to toggle source
# File lib/nametrainer/rng.rb, line 30 def rand x = Kernel.rand interval = @limits.find_index {|limit| x < limit } interval || (@size - 1) end
Private Instance Methods
normalized_limits()
click to toggle source
Returns a list of upper limits normalized to the range [0;1).
# File lib/nametrainer/rng.rb, line 57 def normalized_limits max = raw_limits.last raw_limits.map {|limit| limit.to_f / max } end
raw_limits()
click to toggle source
Returns a list of upper limits for all intervals.
# File lib/nametrainer/rng.rb, line 47 def raw_limits limits = weighting_factors.dup 1.upto(@size - 1) do |index| limits[index] = limits[index-1] + limits[index] end limits end
weighting_factors()
click to toggle source
Returns a list of weighting factors from @weighting_factor to 1.
# File lib/nametrainer/rng.rb, line 40 def weighting_factors delta = (@weighting_factor - 1).to_f / (@size - 1) Array.new(@size) {|index| @weighting_factor - index * delta} end