Class: EnvelopeAlloc::RandomGaussian
- Inherits:
-
Object
- Object
- EnvelopeAlloc::RandomGaussian
- Defined in:
- lib/envelope_alloc/gaussian.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(mean, stddev, rand_helper = lambda {Kernel.rand}) ⇒ RandomGaussian
constructor
A new instance of RandomGaussian.
- #rand ⇒ Object
Constructor Details
#initialize(mean, stddev, rand_helper = lambda {Kernel.rand}) ⇒ RandomGaussian
Returns a new instance of RandomGaussian.
13 14 15 16 17 18 19 |
# File 'lib/envelope_alloc/gaussian.rb', line 13 def initialize(mean, stddev, rand_helper = lambda {Kernel.rand}) @rand_helper = rand_helper @mean = mean @stddev = stddev @valid = false @next = 0 end |
Class Method Details
.gaussian(mean, stddev, rand) ⇒ Object
4 5 6 7 8 9 10 11 |
# File 'lib/envelope_alloc/gaussian.rb', line 4 def self.gaussian(mean, stddev, rand) rho = Math.sqrt(-2 * Math.log(1 - rand.call)) scale = stddev * rho theta = 2 * Math::PI * rand.call x = mean + scale * Math.cos(theta) y = mean + scale * Math.sin(theta) [x, y] end |
Instance Method Details
#rand ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/envelope_alloc/gaussian.rb', line 21 def rand if @valid @valid = false @next else @valid = true x, y = self.class.gaussian(@mean, @stddev, @rand_helper) @next = y x end end |