Class: RandomDistribution::GaussianSequence

Inherits:
Sequence show all
Defined in:
lib/redshift/util/random.rb

Constant Summary

Constants included from Math

Math::Infinity

Instance Attribute Summary collapse

Attributes inherited from Sequence

#generator

Instance Method Summary collapse

Methods inherited from Sequence

random_pool_seed, random_seed, serial_count

Constructor Details

#initialize(opt = {}) ⇒ GaussianSequence

Returns a new instance of GaussianSequence.



123
124
125
126
127
128
129
130
# File 'lib/redshift/util/random.rb', line 123

def initialize opt = {}
  super
  @mean = Float(opt[:mean] || 0)
  @stdev = Float(opt[:stdev] || 1)
  @min = opt[:min]; @min = Float(@min) if @min
  @max = opt[:max]; @max = Float(@max) if @max
  @nextnext = nil
end

Instance Attribute Details

#maxObject (readonly)

Returns the value of attribute max.



121
122
123
# File 'lib/redshift/util/random.rb', line 121

def max
  @max
end

#meanObject (readonly)

Returns the value of attribute mean.



121
122
123
# File 'lib/redshift/util/random.rb', line 121

def mean
  @mean
end

#minObject (readonly)

Returns the value of attribute min.



121
122
123
# File 'lib/redshift/util/random.rb', line 121

def min
  @min
end

#stdevObject (readonly)

Returns the value of attribute stdev.



121
122
123
# File 'lib/redshift/util/random.rb', line 121

def stdev
  @stdev
end

Instance Method Details

#nextObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/redshift/util/random.rb', line 132

def next
  if @nextnext
    result = @mean + @nextnext*@stdev
    @nextnext = nil
  
  else
    begin
      v1 = 2 * super - 1
      v2 = 2 * super - 1
      rsq = v1*v1 + v2*v2
    end while rsq >= 1 || rsq == 0

    fac = sqrt(-2*log(rsq) / rsq)
    @nextnext = v1*fac
    result = @mean + v2*fac*@stdev
  end
  
  if @min and result < @min
    result = @min
  elsif @max and result > @max
    result = @max
  end
  
  return result
end