--- /usr/lib/python2.3/random.py 2006-10-16 21:36:45.000000000 +0200 +++ random.py 2007-03-15 14:06:59.000000000 +0100 @@ -619,6 +619,22 @@ u = 1.0 - self.random() return alpha * pow(-_log(u), 1.0/beta) +## -------------------- Triangular -------------------- + def triangular(self, left, center, right): + """Triangular distribution. + + Generates a random value bounded by left and right, with most likely + value center. + """ + # Algorithm: Approaches for Random Number Generation, Duncan Temple Lang + # http://eeyore.ucdavis.edu/stat141/Notes/RNG.pdf + x = self.random() + if x < (center-left)/(right-left): + x = _sqrt(x*(center-left)*(right-left)) + left + else: + x = right - _sqrt((1-x)*(right-left)*(right-center)) + return x + ## -------------------- Wichmann-Hill ------------------- class WichmannHill(Random): @@ -835,6 +851,7 @@ betavariate = _inst.betavariate paretovariate = _inst.paretovariate weibullvariate = _inst.weibullvariate +triangular = _inst.triangular getstate = _inst.getstate setstate = _inst.setstate jumpahead = _inst.jumpahead