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.



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

#pObject (readonly)

Returns the value of attribute p.



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

def p
  @p
end

Instance Method Details

#nextObject



450
451
452
# File 'lib/random_variates.rb', line 450

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