class RandomSet::Gaussian

Generates a Gaussian (normally distributed) random number, using the given mean and standard deviation.

Source: stackoverflow.com/questions/5825680/code-to-generate-gaussian-normally-distributed-random-numbers-in-ruby

Public Class Methods

new(mean, stddev, rand_helper = lambda { Kernel.rand }) click to toggle source

Initializes the generator.

@param [Float] mean The mean. @param [Float] stddev The standard deviation. @param [Proc] rand_helper

A proc used to generate the random number. This is Ruby's +rand+ function by default.
# File lib/random_set/gaussian.rb, line 15
def initialize(mean, stddev, rand_helper = lambda { Kernel.rand })
  @rand_helper = rand_helper
  @mean = mean
  @stddev = stddev
  @valid = false
  @next = 0
end

Private Class Methods

gaussian(mean, stddev, rand) click to toggle source
# File lib/random_set/gaussian.rb, line 37
def self.gaussian(mean, stddev, rand)
  theta = 2 * Math::PI * rand.call
  rho = Math.sqrt(-2 * Math.log(1 - rand.call))
  scale = stddev * rho
  x = mean + scale * Math.cos(theta)
  y = mean + scale * Math.sin(theta)
  return x, y
end

Public Instance Methods

next() click to toggle source
# File lib/random_set/gaussian.rb, line 23
def next
  if @valid then
    @valid = false
    return @next
  else
    @valid = true
    x, y = self.class.gaussian(@mean, @stddev, @rand_helper)
    @next = y
    return x
  end
end