]> granicus.if.org Git - python/commitdiff
Adopt Nick's suggestion for useful default arguments.
authorRaymond Hettinger <python@rcn.com>
Sun, 23 Mar 2008 19:37:53 +0000 (19:37 +0000)
committerRaymond Hettinger <python@rcn.com>
Sun, 23 Mar 2008 19:37:53 +0000 (19:37 +0000)
Clean-up floating point issues by adding true division and float constants.

Doc/library/random.rst
Lib/random.py

index 505cc81a90301eb316be17ba7fd391ee291dfbd6..a064ac42464a46575da55533314b1e798f765abc 100644 (file)
@@ -193,7 +193,12 @@ be found in any statistics text.
 .. function:: triangular(low, high, mode)
 
    Return a random floating point number *N* such that ``low <= N < high``
-   and with the specified *mode* between those bounds.  
+   and with the specified *mode* between those bounds.
+
+   If *mode* is not specified or is ``None``, it defaults to the midpoint
+   between the upper and lower bounds, producing a symmetric distribution.
+
+   The default values for *low* and *high* are zero and one.
 
 .. function:: betavariate(alpha, beta)
 
index 13125e2af283a098dfbfea759a83f87e3aaf02f9..bd8679fea93568d5a7122f108f5861b8d0b46a50 100644 (file)
@@ -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