Class: ERV::UniformDistribution

Inherits:
Distribution show all
Defined in:
lib/erv/uniform_distribution.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ UniformDistribution

Returns a new instance of UniformDistribution.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
# File 'lib/erv/uniform_distribution.rb', line 8

def initialize(opts={})
  super(opts)

  raise ArgumentError unless opts[:max_value]
  max = opts[:max_value].to_f
  @min = opts[:min_value].try(:to_f) || 0.0
  @range = max - @min
end

Instance Method Details

#sampleObject



17
18
19
20
21
22
# File 'lib/erv/uniform_distribution.rb', line 17

def sample
  # starting from a random variable X ~ U(0,1), which is provided by the
  # RNG, we can obtain a random variable Y ~ U(a,b) through location-scale
  # transformation: Y = a + (b-a) X. see [KROESE11], section 3.1.2.2.
  @min + @range * @rng.rand
end