def stdgamma(self, alpha, ainv, bbb, ccc): # ainv = sqrt(2 * alpha - 1) # bbb = alpha - log(4) # ccc = alpha + ainv random = self.random if alpha <= 0.0: raise ValueError, 'stdgamma: alpha must be > 0.0' if alpha > 1.0: # Uses R.C.H. Cheng, "The generation of Gamma # variables with non-integral shape parameters", # Applied Statistics, (1977), 26, No. 1, p71-74 while 1: u1 = random() u2 = random() v = _log(u1/(1.0-u1))/ainv x = alpha*_exp(v) z = u1*u1*u2 r = bbb+ccc*v-x if r + SG_MAGICCONST - 4.5*z >= 0.0 or r >= _log(z): return x elif alpha == 1.0: # expovariate(1) u = random() while u <= 1e-7: u = random() return -_log(u) else: # alpha is between 0 and 1 (exclusive) # Uses ALGORITHM GS of Statistical Computing - Kennedy & Gentle while 1: u = random() b = (_e + alpha)/_e p = b*u if p <= 1.0: x = pow(p, 1.0/alpha) else: # p > 1 x = -_log((b-p)/alpha) u1 = random() if not (((p <= 1.0) and (u1 > _exp(-x))) or ((p > 1) and (u1 > pow(x, alpha - 1.0)))): break return x
def stdgamma(self, alpha, ainv, bbb, ccc): # ainv = sqrt(2 * alpha - 1) # bbb = alpha - log(4) # ccc = alpha + ainv random = self.random if alpha <= 0.0: raise ValueError, 'stdgamma: alpha must be > 0.0' if alpha > 1.0: # Uses R.C.H. Cheng, "The generation of Gamma # variables with non-integral shape parameters", # Applied Statistics, (1977), 26, No. 1, p71-74 while 1: u1 = random() u2 = random() v = _log(u1 / (1.0 - u1)) / ainv x = alpha * _exp(v) z = u1 * u1 * u2 r = bbb + ccc * v - x if r + SG_MAGICCONST - 4.5 * z >= 0.0 or r >= _log(z): return x elif alpha == 1.0: # expovariate(1) u = random() while u <= 1e-7: u = random() return -_log(u) else: # alpha is between 0 and 1 (exclusive) # Uses ALGORITHM GS of Statistical Computing - Kennedy & Gentle while 1: u = random() b = (_e + alpha) / _e p = b * u if p <= 1.0: x = pow(p, 1.0 / alpha) else: # p > 1 x = -_log((b - p) / alpha) u1 = random() if not (((p <= 1.0) and (u1 > _exp(-x))) or ((p > 1) and (u1 > pow(x, alpha - 1.0)))): break return x
def gauss(self, mu, sigma): # When x and y are two variables from [0, 1), uniformly # distributed, then # # cos(2*pi*x)*sqrt(-2*log(1-y)) # sin(2*pi*x)*sqrt(-2*log(1-y)) # # are two *independent* variables with normal distribution # (mu = 0, sigma = 1). # (Lambert Meertens) # (corrected version; bug discovered by Mike Miller, fixed by LM) # Multithreading note: When two threads call this function # simultaneously, it is possible that they will receive the # same return value. The window is very small though. To # avoid this, you have to use a lock around all calls. (I # didn't want to slow this down in the serial case by using a # lock here.) random = self.random z = self.gauss_next self.gauss_next = None if z is None: x2pi = random() * TWOPI g2rad = _sqrt(-2.0 * _log(1.0 - random())) z = _cos(x2pi) * g2rad self.gauss_next = _sin(x2pi) * g2rad return mu + z*sigma
def gauss(self, mu, sigma): # When x and y are two variables from [0, 1), uniformly # distributed, then # # cos(2*pi*x)*sqrt(-2*log(1-y)) # sin(2*pi*x)*sqrt(-2*log(1-y)) # # are two *independent* variables with normal distribution # (mu = 0, sigma = 1). # (Lambert Meertens) # (corrected version; bug discovered by Mike Miller, fixed by LM) # Multithreading note: When two threads call this function # simultaneously, it is possible that they will receive the # same return value. The window is very small though. To # avoid this, you have to use a lock around all calls. (I # didn't want to slow this down in the serial case by using a # lock here.) random = self.random z = self.gauss_next self.gauss_next = None if z is None: x2pi = random() * TWOPI g2rad = _sqrt(-2.0 * _log(1.0 - random())) z = _cos(x2pi) * g2rad self.gauss_next = _sin(x2pi) * g2rad return mu + z * sigma
def expovariate(self, lambd): # lambd: rate lambd = 1/mean # ('lambda' is a Python reserved word) random = self.random u = random() while u <= 1e-7: u = random() return -_log(u)/lambd
def expovariate(self, lambd): # lambd: rate lambd = 1/mean # ('lambda' is a Python reserved word) random = self.random u = random() while u <= 1e-7: u = random() return -_log(u) / lambd
def normalvariate(self, mu, sigma): # mu = mean, sigma = standard deviation # Uses Kinderman and Monahan method. Reference: Kinderman, # A.J. and Monahan, J.F., "Computer generation of random # variables using the ratio of uniform deviates", ACM Trans # Math Software, 3, (1977), pp257-260. random = self.random while 1: u1 = random() u2 = random() z = NV_MAGICCONST*(u1-0.5)/u2 zz = z*z/4.0 if zz <= -_log(u2): break return mu + z*sigma
def normalvariate(self, mu, sigma): # mu = mean, sigma = standard deviation # Uses Kinderman and Monahan method. Reference: Kinderman, # A.J. and Monahan, J.F., "Computer generation of random # variables using the ratio of uniform deviates", ACM Trans # Math Software, 3, (1977), pp257-260. random = self.random while 1: u1 = random() u2 = random() z = NV_MAGICCONST * (u1 - 0.5) / u2 zz = z * z / 4.0 if zz <= -_log(u2): break return mu + z * sigma
def weibullvariate(self, alpha, beta): # Jain, pg. 499; bug fix courtesy Bill Arms u = self.random() return alpha * pow(-_log(u), 1.0/beta)
"stdgamma","gauss","betavariate","paretovariate","weibullvariate", "getstate","setstate","jumpahead","whseed"] def _verify(name, computed, expected): if abs(computed - expected) > 1e-7: raise ValueError( "computed value for %s deviates too much " "(computed %g, expected %g)" % (name, computed, expected)) NV_MAGICCONST = 4 * _exp(-0.5)/_sqrt(2.0) _verify('NV_MAGICCONST', NV_MAGICCONST, 1.71552776992141) TWOPI = 2.0*_pi _verify('TWOPI', TWOPI, 6.28318530718) LOG4 = _log(4.0) _verify('LOG4', LOG4, 1.38629436111989) SG_MAGICCONST = 1.0 + _log(4.5) _verify('SG_MAGICCONST', SG_MAGICCONST, 2.50407739677627) del _verify # Translated by Guido van Rossum from C source provided by # Adrian Baddeley. class Random: VERSION = 1 # used by getstate/setstate def __init__(self, x=None):
def weibullvariate(self, alpha, beta): # Jain, pg. 499; bug fix courtesy Bill Arms u = self.random() return alpha * pow(-_log(u), 1.0 / beta)
def _verify(name, computed, expected): if abs(computed - expected) > 1e-7: raise ValueError("computed value for %s deviates too much " "(computed %g, expected %g)" % (name, computed, expected)) NV_MAGICCONST = 4 * _exp(-0.5) / _sqrt(2.0) _verify('NV_MAGICCONST', NV_MAGICCONST, 1.71552776992141) TWOPI = 2.0 * _pi _verify('TWOPI', TWOPI, 6.28318530718) LOG4 = _log(4.0) _verify('LOG4', LOG4, 1.38629436111989) SG_MAGICCONST = 1.0 + _log(4.5) _verify('SG_MAGICCONST', SG_MAGICCONST, 2.50407739677627) del _verify # Translated by Guido van Rossum from C source provided by # Adrian Baddeley. class Random: VERSION = 1 # used by getstate/setstate