Class: Geometric

Inherits:
Object
  • Object
show all
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

Attributes included from RV_Generator

#generator

Instance Method Summary collapse

Methods included from RV_Generator

#each, #next

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

#pObject (readonly)

Returns the value of attribute p.



389
390
391
# File 'lib/random_variates.rb', line 389

def p
  @p
end