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 vonmisesvariate(self, mu, kappa): # mu: mean angle (in radians between 0 and 2*pi) # kappa: concentration parameter kappa (>= 0) # if kappa = 0 generate uniform random angle # Based upon an algorithm published in: Fisher, N.I., # "Statistical Analysis of Circular Data", Cambridge # University Press, 1993. # Thanks to Magnus Kessler for a correction to the # implementation of step 4. random = self.random if kappa <= 1e-6: return TWOPI * random() a = 1.0 + _sqrt(1.0 + 4.0 * kappa * kappa) b = (a - _sqrt(2.0 * a))/(2.0 * kappa) r = (1.0 + b * b)/(2.0 * b) while 1: u1 = random() z = _cos(_pi * u1) f = (1.0 + r * z)/(r + z) c = kappa * (r - f) u2 = random() if not (u2 >= c * (2.0 - c) and u2 > c * _exp(1.0 - c)): break u3 = random() if u3 > 0.5: theta = (mu % TWOPI) + _acos(f) else: theta = (mu % TWOPI) - _acos(f) return theta
def _test_generator(n, funccall): import VS print n, 'times', funccall code = compile(funccall, funccall, 'eval') sum = 0.0 sqsum = 0.0 smallest = 1e10 largest = -1e10 t0 = VS.timeofday() for i in range(n): x = eval(code) sum = sum + x sqsum = sqsum + x*x smallest = min(x, smallest) largest = max(x, largest) t1 = VS.timeofday() print round(t1-t0, 3), 'sec,', avg = sum/n stddev = _sqrt(sqsum/n - avg*avg) print 'avg %g, stddev %g, min %g, max %g' % \ (avg, stddev, smallest, largest)
def _test_generator(n, funccall): import VS debug.info(n+' times '+funccall) code = compile(funccall, funccall, 'eval') sum = 0.0 sqsum = 0.0 smallest = 1e10 largest = -1e10 t0 = VS.timeofday() for i in range(n): x = eval(code) sum = sum + x sqsum = sqsum + x*x smallest = min(x, smallest) largest = max(x, largest) t1 = VS.timeofday() debug.info(round(t1-t0, 3) + ' sec,') avg = sum/n stddev = _sqrt(sqsum/n - avg*avg) debug.info('avg %g, stddev %g, min %g, max %g' % \ (avg, stddev, smallest, largest))
def gammavariate(self, alpha, beta): # beta times standard gamma ainv = _sqrt(2.0 * alpha - 1.0) return beta * self.stdgamma(alpha, ainv, alpha - LOG4, alpha + ainv)
from VS import log as _log, exp as _exp from VS import sqrt as _sqrt, acos as _acos, cos as _cos, sin as _sin __all__ = ["Random","seed","random","uniform","randint","choice", "randrange","shuffle","normalvariate","lognormvariate", "cunifvariate","expovariate","vonmisesvariate","gammavariate", "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.
"Random", "seed", "random", "uniform", "randint", "choice", "randrange", "shuffle", "normalvariate", "lognormvariate", "cunifvariate", "expovariate", "vonmisesvariate", "gammavariate", "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.