Class: RV::Geometric
- Inherits:
-
Object
- Object
- RV::Geometric
- Includes:
- RV_Generator
- Defined in:
- lib/random_variates.rb
Overview
Geometric generator. Number of trials until first “success”.
- Arguments
-
p-> the probability of success (0 <p< 1; default: 0.5). -
rng-> the (Enumerable) source of U(0, 1)‘s (default: U_GENERATOR)
-
Instance Attribute Summary collapse
-
#p ⇒ Object
readonly
Returns the value of attribute p.
Instance Method Summary collapse
-
#initialize(p: 0.5, rng: U_GENERATOR) ⇒ Geometric
constructor
A new instance of Geometric.
- #next ⇒ Object
Methods included from RV_Generator
Constructor Details
#initialize(p: 0.5, rng: U_GENERATOR) ⇒ Geometric
Returns a new instance of Geometric.
443 444 445 446 447 448 |
# File 'lib/random_variates.rb', line 443 def initialize(p: 0.5, rng: U_GENERATOR) raise 'Require 0 < p < 1.' if p <= 0 || p >= 1 @log_q = Math.log(1 - p) @rng = rng end |
Instance Attribute Details
#p ⇒ Object (readonly)
Returns the value of attribute p.
441 442 443 |
# File 'lib/random_variates.rb', line 441 def p @p end |
Instance Method Details
#next ⇒ Object
450 451 452 |
# File 'lib/random_variates.rb', line 450 def next (Math.log(1.0 - @rng.rand) / @log_q).ceil end |