class RV::BoxMuller

Alternate normal/Gaussian random variate generator with specified mean and +standard deviation+. Defaults to a standard normal.

Arguments
  • mu -> the expected value (default: 0).

  • sigma -> the standard deviation (default: 1).

  • rng -> the (Enumerable) source of U(0, 1)'s (default: U_GENERATOR)

Constants

TWO_PI

Attributes

mu[R]
sigma[R]

Public Class Methods

new(mu: 0.0, sigma: 1.0, rng: U_GENERATOR) click to toggle source
# File lib/random_variates.rb, line 234
def initialize(mu: 0.0, sigma: 1.0, rng: U_GENERATOR)
  raise 'Standard deviation must be positive.' if sigma <= 0

  @mu = mu
  @sigma = sigma
  @rng = rng
  @need_new_pair = false
  # @next_norm = 0.0
end

Public Instance Methods

mu=(mu) click to toggle source
# File lib/random_variates.rb, line 260
def mu=(mu)
  raise 'mu must be a number.' unless mu.is_a? Numeric
  @mu = mu
end
next() click to toggle source
# File lib/random_variates.rb, line 244
def next
  if @need_new_pair ^= true
    theta = TWO_PI * @rng.next
    d = @sigma * Math.sqrt(-2.0 * Math.log(@rng.next))
    @next_norm = @mu + d * Math.sin(theta)
    @mu + d * Math.cos(theta)
  else
    @next_norm
  end
end
sigma=(sigma) click to toggle source
# File lib/random_variates.rb, line 255
def sigma=(sigma)
  raise 'sigma must be a number.' unless sigma.is_a? Numeric
  @sigma = sigma
end