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.
234 235 236 237 238 239 240 241 242 |
# File 'lib/random_variates.rb', line 234 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 # @next_norm = 0.0 end |
Instance Attribute Details
#mu ⇒ Object
Returns the value of attribute mu.
232 233 234 |
# File 'lib/random_variates.rb', line 232 def mu @mu end |
#sigma ⇒ Object
Returns the value of attribute sigma.
232 233 234 |
# File 'lib/random_variates.rb', line 232 def sigma @sigma end |
Instance Method Details
#next ⇒ Object
244 245 246 247 248 249 250 251 252 253 |
# File 'lib/random_variates.rb', line 244 def next if @need_new_pair ^= true theta = TWO_PI * @rng.next d = @sigma * Math.sqrt(-2.0 * Math.log(@rng.next)) @next_norm = @mu + d * Math.sin(theta) @mu + d * Math.cos(theta) else @next_norm end end |