class RailsDataExplorer::Statistics::RngGaussian

Responsibilities:

* Provide random numeric data, following a gaussian distribution.

From stackoverflow.com/a/9266488

Public Class Methods

new(mean = 0.0, sd = 1.0, rng = lambda { Kernel.rand }) click to toggle source

@param mean [Float] the expected mean @param sd [Float] the expected standard deviation @param rng [Proc, optional] a random number generator

# File lib/rails_data_explorer/statistics/rng_gaussian.rb, line 14
def initialize(mean = 0.0, sd = 1.0, rng = lambda { Kernel.rand })
  @mean, @sd, @rng = mean, sd, rng
  @compute_next_pair = false
end

Public Instance Methods

rand() click to toggle source

Returns random numbers with a gaussian distribution.

# File lib/rails_data_explorer/statistics/rng_gaussian.rb, line 20
def rand
  if (@compute_next_pair = !@compute_next_pair)
    # Compute a pair of random values with normal distribution.
    # See http://en.wikipedia.org/wiki/Box-Muller_transform
    theta = 2 * Math::PI * @rng.call
    scale = @sd * Math.sqrt(-2 * Math.log(1 - @rng.call))
    @g1 = @mean + scale * Math.sin(theta)
    @g0 = @mean + scale * Math.cos(theta)
  else
    @g1
  end
end