Class: RV::Exponential

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

Overview

Exponential random variate generator with specified rate or mean. One and only one of rate or mean should be specified.

Arguments
  • rate -> the rate of occurrences per unit time (default: nil).

  • mean -> the expected value of the distribution (default: nil).

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RV_Generator

#each

Constructor Details

#initialize(rate: nil, mean: nil, rng: U_GENERATOR) ⇒ Exponential

Returns a new instance of Exponential.



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/random_variates.rb', line 138

def initialize(rate: nil, mean: nil, rng: U_GENERATOR)
  raise 'Rate must be positive.' if rate && rate <= 0
  raise 'Mean must be positive.' if mean && mean <= 0
  unless rate.nil? ^ mean.nil?
    raise 'Supply one and only one of mean or rate'
  end

  if rate.nil?
    @mean = mean
    @rate = 1.0 / mean
  else
    @mean = 1.0 / rate
    @rate = rate
  end
  @rng = rng
end

Instance Attribute Details

#meanObject

Returns the value of attribute mean.



136
137
138
# File 'lib/random_variates.rb', line 136

def mean
  @mean
end

#rateObject

Returns the value of attribute rate.



136
137
138
# File 'lib/random_variates.rb', line 136

def rate
  @rate
end

Instance Method Details

#nextObject



155
156
157
# File 'lib/random_variates.rb', line 155

def next
  -@mean * Math.log(@rng.rand)
end