Class: RV::BoxMuller
- Inherits:
-
Object
- Object
- RV::BoxMuller
- Includes:
- RV_Generator
- Defined in:
- lib/random_variates.rb
Overview
Alternate normal/Gaussian random variate generator with specified mean
and standard deviation. Defaults to a standard normal.
- Arguments
-
mu
-> the expected value (default: 0). -
sigma
-> the standard deviation (default: 1). -
rng
-> the (Enumerable
) source of U(0, 1)‘s (default: U_GENERATOR)
-
Constant Summary collapse
- TWO_PI =
2.0 * Math::PI
Instance Attribute Summary collapse
-
#mu ⇒ Object
Returns the value of attribute mu.
-
#sigma ⇒ Object
Returns the value of attribute sigma.
Instance Method Summary collapse
-
#initialize(mu: 0.0, sigma: 1.0, rng: U_GENERATOR) ⇒ BoxMuller
constructor
A new instance of BoxMuller.
- #next ⇒ Object
Methods included from RV_Generator
Constructor Details
#initialize(mu: 0.0, sigma: 1.0, rng: U_GENERATOR) ⇒ BoxMuller
Returns a new instance of BoxMuller.
239 240 241 242 243 244 245 246 |
# File 'lib/random_variates.rb', line 239 def initialize(mu: 0.0, sigma: 1.0, rng: U_GENERATOR) raise 'Standard deviation must be positive.' if sigma <= 0 @mu = mu @sigma = sigma @rng = rng @need_new_pair = false end |
Instance Attribute Details
#mu ⇒ Object
Returns the value of attribute mu.
237 238 239 |
# File 'lib/random_variates.rb', line 237 def mu @mu end |
#sigma ⇒ Object
Returns the value of attribute sigma.
237 238 239 |
# File 'lib/random_variates.rb', line 237 def sigma @sigma end |
Instance Method Details
#next ⇒ Object
248 249 250 251 252 253 254 255 256 257 |
# File 'lib/random_variates.rb', line 248 def next if @need_new_pair ^= true theta = TWO_PI * @rng.rand d = @sigma * Math.sqrt(-2.0 * Math.log(@rng.rand)) @next_norm = @mu + d * Math.sin(theta) @mu + d * Math.cos(theta) else @next_norm end end |