Class: Starter::Random::GaussianSequence
- Defined in:
- lib/starter/random.rb
Instance Attribute Summary collapse
-
#max ⇒ Object
readonly
Returns the value of attribute max.
-
#mean ⇒ Object
readonly
Returns the value of attribute mean.
-
#min ⇒ Object
readonly
Returns the value of attribute min.
-
#stdev ⇒ Object
readonly
Returns the value of attribute stdev.
Attributes inherited from Sequence
Instance Method Summary collapse
-
#initialize(opt = {}) ⇒ GaussianSequence
constructor
A new instance of GaussianSequence.
- #next ⇒ Object
Methods inherited from Sequence
random_pool_seed, random_seed, serial_count
Constructor Details
#initialize(opt = {}) ⇒ GaussianSequence
Returns a new instance of GaussianSequence.
131 132 133 134 135 136 137 138 |
# File 'lib/starter/random.rb', line 131 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
#max ⇒ Object (readonly)
Returns the value of attribute max.
129 130 131 |
# File 'lib/starter/random.rb', line 129 def max @max end |
#mean ⇒ Object (readonly)
Returns the value of attribute mean.
129 130 131 |
# File 'lib/starter/random.rb', line 129 def mean @mean end |
#min ⇒ Object (readonly)
Returns the value of attribute min.
129 130 131 |
# File 'lib/starter/random.rb', line 129 def min @min end |
#stdev ⇒ Object (readonly)
Returns the value of attribute stdev.
129 130 131 |
# File 'lib/starter/random.rb', line 129 def stdev @stdev end |
Instance Method Details
#next ⇒ Object
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/starter/random.rb', line 140 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 |