Class: RV::Poisson

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

Overview

Poisson generator.

Arguments
  • rate -> expected number per unit time/distance (rate > 0; default: 1).

  • 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: 1.0, rng: U_GENERATOR) ⇒ Poisson

Returns a new instance of Poisson.



409
410
411
412
413
414
415
# File 'lib/random_variates.rb', line 409

def initialize(rate: 1.0, rng: U_GENERATOR)
  raise 'rate must be positive.' if rate <= 0

  @rate = rate
  @threshold = Math.exp(-rate)
  @rng = rng
end

Instance Attribute Details

#rateObject

Returns the value of attribute rate.



407
408
409
# File 'lib/random_variates.rb', line 407

def rate
  @rate
end

Instance Method Details

#nextObject



424
425
426
427
428
429
# File 'lib/random_variates.rb', line 424

def next
  count = 0
  product = 1.0
  count += 1 until (product *= @rng.rand) < @threshold
  count
end