class RV::Normal
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)
-
Constants
- BOUND
Attributes
mu[R]
sigma[R]
Public Class Methods
new(mu: 0.0, sigma: 1.0, rng: U_GENERATOR)
click to toggle source
# 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
Public Instance Methods
mu=(mu)
click to toggle source
# File lib/random_variates.rb, line 214 def mu=(mu) raise 'mu must be a number.' unless mu.is_a? Numeric @mu = mu end
next()
click to toggle source
# 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
sigma=(sigma)
click to toggle source
# File lib/random_variates.rb, line 209 def sigma=(sigma) raise 'sigma must be a number.' unless sigma.is_a? Numeric @sigma = sigma end