Class: ERV::UniformDistribution

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

Constant Summary

Constants inherited from Distribution

Distribution::DEFAULT_SEED

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ UniformDistribution

Returns a new instance of UniformDistribution.

Raises:

  • (ArgumentError)


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

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

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

Instance Method Details

#sampleObject



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

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