[Python-checkins] r61807 - in python/trunk: Doc/library/random.rst Lib/random.py

raymond.hettinger python-checkins at python.org
Sun Mar 23 20:37:53 CET 2008


Author: raymond.hettinger
Date: Sun Mar 23 20:37:53 2008
New Revision: 61807

Modified:
   python/trunk/Doc/library/random.rst
   python/trunk/Lib/random.py
Log:
Adopt Nick's suggestion for useful default arguments.
Clean-up floating point issues by adding true division and float constants.



Modified: python/trunk/Doc/library/random.rst
==============================================================================
--- python/trunk/Doc/library/random.rst	(original)
+++ python/trunk/Doc/library/random.rst	Sun Mar 23 20:37:53 2008
@@ -193,7 +193,12 @@
 .. 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)
 

Modified: python/trunk/Lib/random.py
==============================================================================
--- python/trunk/Lib/random.py	(original)
+++ python/trunk/Lib/random.py	Sun Mar 23 20:37:53 2008
@@ -39,6 +39,7 @@
 
 """
 
+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 @@
 
 ## -------------------- 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 @@
 
         """
         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
 


More information about the Python-checkins mailing list