Class: ViralSeq::Math::RandomGaussian
- Inherits:
-
Object
- Object
- ViralSeq::Math::RandomGaussian
- Defined in:
- lib/viral_seq/math.rb
Overview
Generate values from the standard normal distribution with given mean and standard deviation
Instance Method Summary collapse
-
#initialize(mean = 0.0, sd = 1.0, rng = lambda { Kernel.rand }) ⇒ RandomGaussian
constructor
generate RandomGaussian instance with given mean and standard deviation.
-
#rand ⇒ Float
generate a random number that falls in the pre-defined gaussian distribution.
Constructor Details
#initialize(mean = 0.0, sd = 1.0, rng = lambda { Kernel.rand }) ⇒ RandomGaussian
generate RandomGaussian instance with given mean and standard deviation
17 18 19 20 |
# File 'lib/viral_seq/math.rb', line 17 def initialize(mean = 0.0, sd = 1.0, rng = lambda { Kernel.rand }) @mean, @sd, @rng = mean, sd, rng @compute_next_pair = false end |
Instance Method Details
#rand ⇒ Float
generate a random number that falls in the pre-defined gaussian distribution
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/viral_seq/math.rb', line 31 def rand if (@compute_next_pair = !@compute_next_pair) theta = 2 * ::Math::PI * @rng.call scale = @sd * ::Math.sqrt(-2 * ::Math.log(1 - @rng.call)) @g1 = @mean + scale * ::Math.sin(theta) @g0 = @mean + scale * ::Math.cos(theta) else @g1 end end |