Class: RV::Poisson
- Inherits:
-
Object
- Object
- RV::Poisson
- 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
-
#rate ⇒ Object
Returns the value of attribute rate.
Instance Method Summary collapse
-
#initialize(rate: 1.0, rng: U_GENERATOR) ⇒ Poisson
constructor
A new instance of Poisson.
- #next ⇒ Object
Methods included from RV_Generator
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
#rate ⇒ Object
Returns the value of attribute rate.
407 408 409 |
# File 'lib/random_variates.rb', line 407 def rate @rate end |
Instance Method Details
#next ⇒ Object
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 |