diff options
author | Raymond Hettinger <python@rcn.com> | 2008-03-23 19:37:53 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2008-03-23 19:37:53 (GMT) |
commit | c4f7bab0a0cd208bcab3c4f6cd8324ed8d08f98e (patch) | |
tree | e9bbc49e42eab1df3707bf2ddd26c1cb38e1b634 /Lib | |
parent | bbc50eafe5cc7d2fa73b5b45eebc573c600db9ac (diff) | |
download | cpython-c4f7bab0a0cd208bcab3c4f6cd8324ed8d08f98e.zip cpython-c4f7bab0a0cd208bcab3c4f6cd8324ed8d08f98e.tar.gz cpython-c4f7bab0a0cd208bcab3c4f6cd8324ed8d08f98e.tar.bz2 |
Adopt Nick's suggestion for useful default arguments.
Clean-up floating point issues by adding true division and float constants.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/random.py | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/Lib/random.py b/Lib/random.py index 13125e2..bd8679f 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -39,6 +39,7 @@ General notes on the underlying Mersenne Twister core generator: """ +from __future__ import division from warnings import warn as _warn from types import MethodType as _MethodType, BuiltinMethodType as _BuiltinMethodType from math import log as _log, exp as _exp, pi as _pi, e as _e, ceil as _ceil @@ -353,7 +354,7 @@ class Random(_random.Random): ## -------------------- triangular -------------------- - def triangular(self, low, high, mode): + def triangular(self, low=0.0, high=1.0, mode=None): """Triangular distribution. Continuous distribution bounded by given lower and upper limits, @@ -363,10 +364,10 @@ class Random(_random.Random): """ u = self.random() - c = (mode - low) / (high - low) + c = 0.5 if mode is None else (mode - low) / (high - low) if u > c: - u = 1 - u - c = 1 - c + u = 1.0 - u + c = 1.0 - c low, high = high, low return low + (high - low) * (u * c) ** 0.5 |