Class: Digiproc::Strategies::GaussianGeneratorBoxMullerStrategy

Inherits:
Object
  • Object
show all
Defined in:
lib/strategies/gaussian/gaussian_generator.rb

Overview

Class for generating random numbers from a Gaussin Population of a given mean or standard deviation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mean = 0, stddev = 1) ⇒ GaussianGeneratorBoxMullerStrategy

Input Args:

mean (Optional)

Numeric (default 0)

stddev (Optional)

Numeric (default 1)



23
24
25
26
27
# File 'lib/strategies/gaussian/gaussian_generator.rb', line 23

def initialize(mean = 0, stddev = 1)
    @mean, @stddev = mean, stddev
    @needs_gen = true
    @next = nil
end

Instance Attribute Details

#meanObject

Returns the value of attribute mean.



17
18
19
# File 'lib/strategies/gaussian/gaussian_generator.rb', line 17

def mean
  @mean
end

#stddevObject

Returns the value of attribute stddev.



17
18
19
# File 'lib/strategies/gaussian/gaussian_generator.rb', line 17

def stddev
  @stddev
end

Class Method Details

.rand2Object

No input args Returns 2 random numbers from a gaussain population with stddev of 1 and mean of 0



10
11
12
13
14
15
# File 'lib/strategies/gaussian/gaussian_generator.rb', line 10

def self.rand2
    uniform_random = Random.new
    r = (-2 * Math.log(1 - uniform_random.rand)) ** 0.5
    theta = 2 * Math::PI * (1 - uniform_random.rand)
    return r * Math.cos(theta), r * Math.sin(theta)    
end

Instance Method Details

#randObject

Get a single random number from a Gaussian distribution with a mean and stddev as defined by @mean and @stddev Use the .rand2 method to get 2 random numbers. Save one, return the other



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/strategies/gaussian/gaussian_generator.rb', line 32

def rand
    if @needs_gen
        x,y = self.class.rand2
        @next = y
        @needs_gen = false
        return self.mean + self.stddev * x
    else
        @needs_gen = true
        return self.mean + self.stddev * @next
    end
end

#rand2Object

Calls the .rand2 method



45
46
47
# File 'lib/strategies/gaussian/gaussian_generator.rb', line 45

def rand2
    self.class.rand2
end