Class: RV::Normal
- Inherits:
-
Object
- Object
- RV::Normal
- Includes:
- RV_Generator
- Defined in:
- lib/random_variates.rb
Overview
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
- BOUND =
2.0 * Math.sqrt(2.0 / Math::E)
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) ⇒ Normal
constructor
A new instance of Normal.
- #next ⇒ Object
Methods included from RV_Generator
Constructor Details
#initialize(mu: 0.0, sigma: 1.0, rng: U_GENERATOR) ⇒ Normal
Returns a new instance of Normal.
188 189 190 191 192 193 194 |
# File 'lib/random_variates.rb', line 188 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 end |
Instance Attribute Details
#mu ⇒ Object
Returns the value of attribute mu.
186 187 188 |
# File 'lib/random_variates.rb', line 186 def mu @mu end |
#sigma ⇒ Object
Returns the value of attribute sigma.
186 187 188 |
# File 'lib/random_variates.rb', line 186 def sigma @sigma end |
Instance Method Details
#next ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/random_variates.rb', line 196 def next while true u = @rng.rand next if u == 0.0 v = BOUND * (@rng.rand - 0.5) x = v / u x_sqr = x * x u_sqr = u * u if 6.0 * x_sqr <= 44.0 - 72.0 * u + 36.0 * u_sqr - 8.0 * u * u_sqr return @sigma * x + @mu elsif x_sqr * u >= 2.0 - 2.0 * u_sqr next elsif x_sqr <= -4.0 * Math.log(u) return @sigma * x + @mu end end end |