Class: Geometric
- Inherits:
-
Object
- Object
- 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.
Attributes included from RV_Generator
Instance Method Summary collapse
-
#initialize(p: 0.5, rng: U_GENERATOR) ⇒ Geometric
constructor
A new instance of Geometric.
Methods included from RV_Generator
Constructor Details
#initialize(p: 0.5, rng: U_GENERATOR) ⇒ Geometric
Returns a new instance of Geometric.
391 392 393 394 395 396 397 398 399 |
# File 'lib/random_variates.rb', line 391 def initialize(p: 0.5, rng: U_GENERATOR) raise 'Require 0 < p < 1.' if p <= 0 || p >= 1 @p = p log_q = Math.log(1 - p) @generator = Enumerator.new do |yielder| loop { yielder << (Math.log(1.0 - rng.next) / log_q).ceil } end end |
Instance Attribute Details
#p ⇒ Object (readonly)
Returns the value of attribute p.
389 390 391 |
# File 'lib/random_variates.rb', line 389 def p @p end |