Class: RailsDataExplorer::Statistics::RngGaussian

Inherits:
Object
  • Object
show all
Defined in:
lib/rails-data-explorer/statistics/rng_gaussian.rb

Instance Method Summary collapse

Constructor Details

#initialize(mean = 0.0, sd = 1.0, rng = lambda { Kernel.rand }) ⇒ RngGaussian

Returns a new instance of RngGaussian.



5
6
7
8
# File 'lib/rails-data-explorer/statistics/rng_gaussian.rb', line 5

def initialize(mean = 0.0, sd = 1.0, rng = lambda { Kernel.rand })
  @mean, @sd, @rng = mean, sd, rng
  @compute_next_pair = false
end

Instance Method Details

#randObject



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rails-data-explorer/statistics/rng_gaussian.rb', line 10

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