require 'pp' module Eco TEST_TIMES = 5000 RANDOM = Eco::Data::Random class Tester include Data def test_normal(test_times = TEST_TIMES) pp "########## Testing Normal Distribution ###########" generator_config = { "range" => (1..100), "decimals" => 0, "balance" => 'normal' } generator = RANDOM::Distribution.new(generator_config) frequency = frequency_sample(generator, test_times) draw_frequency(frequency) end def test_half_normal(test_times = TEST_TIMES) pp "########## Testing Half-Normal Distribution ###########" generator_config = { "range" => (1..100), "decimals" => 0, "balance" => 'half-normal' } generator = RANDOM::Distribution.new(generator_config) frequency = frequency_sample(generator, test_times) draw_frequency(frequency) end def test_forged(test_times = TEST_TIMES) pp "########## Testing Forged Distribution ###########" generator_config = { "range" => (1..100), "decimals" => 0, "balance" => 'forged' } options_weight = {"One" => 10, "Two" => 5, "Three" => 2, "Four" => 1, "Five" => 1} options_config = { #'generator_config' => generator_config, 'options_weight' => options_weight, 'multiple' => false, 'quantity' => 2 } generator = RANDOM::Values.new(options_config) frequency = frequency_sample(generator, test_times) draw_frequency(frequency) end private def frequency_sample (generator, test_times = TEST_TIMES) frequency = Hash.new {|hash, key| hash[key]=0} test_times.times.map { val = generator.generate "here a nil!!" if !val val = val.round if val.is_a? Numeric frequency[val] += 1 if val } return frequency end def draw_frequency(frequency, generator = nil, histogram: true) #pp frequency vals = frequency.values test_times = vals.inject(0, :+) opts = vals.length raise "no tests to show" if !test_times || test_times < 1 dimension = Math::log10(test_times) draw_unit = (10**(dimension - 1) / (opts * 3)).round total = 0; sum = 0 frequency.keys.sort.each do |k| f = frequency[k] percent = (f * 100 / test_times ).round puts "#{k} (#{percent}/100): #{'*' * (f / draw_unit).round}" if :historgram #puts "#{k} (#{percent}/100): #{'*' * f}" if :historgram puts "#{k} (#{percent}/100): #{f}" if !:historgram total += frequency[k] v = k v = 1 if !k.is_a? Numeric sum += (frequency[k] * v) end summary = "total: #{total}; mean: #{sum / total}" summary += "; draw unit #{draw_unit}" if :historgram puts summary pp "generator weights (config): #{generator.weights}" if generator pp "generator unique (config): #{generator.unique}" if generator end end end