Class: Uniform

Inherits:
Object
  • Object
show all
Includes:
RV_Generator
Defined in:
lib/random_variates.rb

Overview

Generate values uniformly distributed between min and max.

Arguments
  • min -> the lower bound for the range (default: 0).

  • max -> the upper bound for the range (default: 1).

  • rng -> the (Enumerable) source of U(0, 1)‘s (default: U_GENERATOR)

Instance Attribute Summary collapse

Attributes included from RV_Generator

#generator

Instance Method Summary collapse

Methods included from RV_Generator

#each, #next

Constructor Details

#initialize(min: 0.0, max: 1.0, rng: U_GENERATOR) ⇒ Uniform

Returns a new instance of Uniform.



48
49
50
51
52
53
54
55
56
57
# File 'lib/random_variates.rb', line 48

def initialize(min: 0.0, max: 1.0, rng: U_GENERATOR)
  raise 'Max must be greater than min.' if max <= min

  @min = min
  @max = max
  range = max - min
  @generator = Enumerator.new do |yielder|
    loop { yielder << (min + range * rng.next) }
  end
end

Instance Attribute Details

#maxObject (readonly)

Returns the value of attribute max.



46
47
48
# File 'lib/random_variates.rb', line 46

def max
  @max
end

#minObject (readonly)

Returns the value of attribute min.



46
47
48
# File 'lib/random_variates.rb', line 46

def min
  @min
end