Class: RandomBell
- Inherits:
-
Object
- Object
- RandomBell
- Defined in:
- lib/random_bell.rb,
lib/random_bell/version.rb
Constant Summary collapse
- VERSION =
"0.2.0"
Instance Method Summary collapse
-
#initialize(mu: 0.5, sigma: 0.2, range: 0.0..1.0, seed: Random.new_seed, method: :box_muller) ⇒ RandomBell
constructor
A new instance of RandomBell.
- #normal ⇒ Float
- #rand ⇒ Float
- #standard ⇒ Float
- #to_histogram(rows: 20, size: 10 ** 4) ⇒ String
Constructor Details
#initialize(mu: 0.5, sigma: 0.2, range: 0.0..1.0, seed: Random.new_seed, method: :box_muller) ⇒ RandomBell
Returns a new instance of RandomBell.
9 10 11 12 13 14 15 |
# File 'lib/random_bell.rb', line 9 def initialize(mu: 0.5, sigma: 0.2, range: 0.0..1.0, seed: Random.new_seed, method: :box_muller) @mu = mu @sigma = sigma @range = range @rand_generator = Random.new(seed) @method = method end |
Instance Method Details
#normal ⇒ Float
23 24 25 |
# File 'lib/random_bell.rb', line 23 def normal @mu + standard * @sigma end |
#rand ⇒ Float
28 29 30 31 32 33 |
# File 'lib/random_bell.rb', line 28 def rand loop do num = normal return num if @range.include?(num) end end |
#standard ⇒ Float
18 19 20 |
# File 'lib/random_bell.rb', line 18 def standard send(@method) end |
#to_histogram(rows: 20, size: 10 ** 4) ⇒ String
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/random_bell.rb', line 36 def to_histogram(rows: 20, size: 10 ** 4) histogram = "" numbers = (1..size).to_a.map{ rand } klasses, klass_width = [], (@range.max - @range.min).to_f / rows (0..rows).each_cons(2) do |left, right| klasses << Range.new(@range.min + klass_width * left, @range.min + klass_width * right) end divider = (klasses.map{|klass| numbers.select{|num| klass.include?(num) }.size }.max / 50.0).ceil klasses.each do |klass| n = numbers.select{|num| klass.include?(num) }.size / divider histogram << sprintf("%+3.3f", klass.max) << ": " << "*" * n << "\n" end histogram end |