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.
183 184 185 186 187 188 189 |
# File 'lib/random_variates.rb', line 183 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.
181 182 183 |
# File 'lib/random_variates.rb', line 181 def mu @mu end |
#sigma ⇒ Object
Returns the value of attribute sigma.
181 182 183 |
# File 'lib/random_variates.rb', line 181 def sigma @sigma end |
Instance Method Details
#next ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/random_variates.rb', line 191 def next loop do u = @rng.next next if u == 0.0 v = BOUND * (@rng.next - 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 |