Class: Poisson
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
-
#rate ⇒ Object
readonly
Returns the value of attribute rate.
Attributes included from RV_Generator
Instance Method Summary collapse
-
#initialize(rate: 1.0, rng: U_GENERATOR) ⇒ Poisson
constructor
A new instance of Poisson.
Methods included from RV_Generator
Constructor Details
#initialize(rate: 1.0, rng: U_GENERATOR) ⇒ Poisson
Returns a new instance of Poisson.
364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
# File 'lib/random_variates.rb', line 364 def initialize(rate: 1.0, rng: U_GENERATOR) raise 'rate must be positive.' if rate <= 0 @rate = rate threshold = Math.exp(-rate) @generator = Enumerator.new do |yielder| loop do count = 0 product = 1.0 count += 1 until (product *= rng.next) < threshold yielder << count end end end |
Instance Attribute Details
#rate ⇒ Object (readonly)
Returns the value of attribute rate.
362 363 364 |
# File 'lib/random_variates.rb', line 362 def rate @rate end |