mirror of
https://github.com/python/cpython.git
synced 2025-08-01 07:33:08 +00:00
Adopt Nick's suggestion for useful default arguments.
Clean-up floating point issues by adding true division and float constants.
This commit is contained in:
parent
bbc50eafe5
commit
c4f7bab0a0
2 changed files with 11 additions and 5 deletions
|
@ -195,6 +195,11 @@ be found in any statistics text.
|
||||||
Return a random floating point number *N* such that ``low <= N < high``
|
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)
|
.. function:: betavariate(alpha, beta)
|
||||||
|
|
||||||
Beta distribution. Conditions on the parameters are ``alpha > 0`` and ``beta >
|
Beta distribution. Conditions on the parameters are ``alpha > 0`` and ``beta >
|
||||||
|
|
|
@ -39,6 +39,7 @@ General notes on the underlying Mersenne Twister core generator:
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from __future__ import division
|
||||||
from warnings import warn as _warn
|
from warnings import warn as _warn
|
||||||
from types import MethodType as _MethodType, BuiltinMethodType as _BuiltinMethodType
|
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
|
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 --------------------
|
## -------------------- triangular --------------------
|
||||||
|
|
||||||
def triangular(self, low, high, mode):
|
def triangular(self, low=0.0, high=1.0, mode=None):
|
||||||
"""Triangular distribution.
|
"""Triangular distribution.
|
||||||
|
|
||||||
Continuous distribution bounded by given lower and upper limits,
|
Continuous distribution bounded by given lower and upper limits,
|
||||||
|
@ -363,10 +364,10 @@ class Random(_random.Random):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
u = self.random()
|
u = self.random()
|
||||||
c = (mode - low) / (high - low)
|
c = 0.5 if mode is None else (mode - low) / (high - low)
|
||||||
if u > c:
|
if u > c:
|
||||||
u = 1 - u
|
u = 1.0 - u
|
||||||
c = 1 - c
|
c = 1.0 - c
|
||||||
low, high = high, low
|
low, high = high, low
|
||||||
return low + (high - low) * (u * c) ** 0.5
|
return low + (high - low) * (u * c) ** 0.5
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue