[Python-Dev] [Python-checkins] r61796 - in python/trunk: Doc/library/random.rst Lib/random.py Lib/test/test_random.py Misc/NEWS
Nick Coghlan
ncoghlan at gmail.com
Sun Mar 23 15:56:03 CET 2008
> +## -------------------- triangular --------------------
> +
> + def triangular(self, low, high, mode):
> + """Triangular distribution.
> +
> + Continuous distribution bounded by given lower and upper limits,
> + and having a given mode value in-between.
> +
> + http://en.wikipedia.org/wiki/Triangular_distribution
> +
> + """
> + u = self.random()
> + c = (mode - low) / (high - low)
> + if u > c:
> + u = 1 - u
> + c = 1 - c
> + low, high = high, low
> + return low + (high - low) * (u * c) ** 0.5
> +
Would it be worth having default values on the parameters for this?
def triangular(self, low=0, high=1, mode=None):
u = self.random()
if mode is None:
c = 0.5
else:
c = (mode - low) / (high - low)
if u > c:
u = 1 - u
c = 1 - c
low, high = high, low
return low + (high - low) * (u * c) ** 0.5
At the very least, supporting mode=None would be convenient when you
want a symmetric triangular distribution - having to calculate the
median, pass it in as the mode, then have the function reverse that to
get 0.5 seems a little wasteful.
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
---------------------------------------------------------------
http://www.boredomandlaziness.org
More information about the Python-Dev
mailing list