Class: RV::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

Instance Method Summary collapse

Methods included from RV_Generator

#each

Constructor Details

#initialize(p: 0.5, rng: U_GENERATOR) ⇒ Geometric

Returns a new instance of Geometric.



432
433
434
435
436
437
438
# File 'lib/random_variates.rb', line 432

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)
  @rng = rng
end

Instance Attribute Details

#pObject (readonly)

Returns the value of attribute p.



430
431
432
# File 'lib/random_variates.rb', line 430

def p
  @p
end

Instance Method Details

#nextObject



440
441
442
# File 'lib/random_variates.rb', line 440

def next
  (Math.log(1.0 - @rng.next) / @log_q).ceil
end