mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
[3.12] More informative docstrings in the random module (gh-109745) (#109905)
More informative docstrings in the random module (gh-109745)
(cherry picked from commit 19bf398695
)
Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
This commit is contained in:
parent
b723b8a13d
commit
b0e377f694
1 changed files with 34 additions and 5 deletions
|
@ -492,7 +492,14 @@ class Random(_random.Random):
|
||||||
## -------------------- real-valued distributions -------------------
|
## -------------------- real-valued distributions -------------------
|
||||||
|
|
||||||
def uniform(self, a, b):
|
def uniform(self, a, b):
|
||||||
"Get a random number in the range [a, b) or [a, b] depending on rounding."
|
"""Get a random number in the range [a, b) or [a, b] depending on rounding.
|
||||||
|
|
||||||
|
The mean (expected value) and variance of the random variable are:
|
||||||
|
|
||||||
|
E[X] = (a + b) / 2
|
||||||
|
Var[X] = (b - a) ** 2 / 12
|
||||||
|
|
||||||
|
"""
|
||||||
return a + (b - a) * self.random()
|
return a + (b - a) * self.random()
|
||||||
|
|
||||||
def triangular(self, low=0.0, high=1.0, mode=None):
|
def triangular(self, low=0.0, high=1.0, mode=None):
|
||||||
|
@ -503,6 +510,11 @@ class Random(_random.Random):
|
||||||
|
|
||||||
http://en.wikipedia.org/wiki/Triangular_distribution
|
http://en.wikipedia.org/wiki/Triangular_distribution
|
||||||
|
|
||||||
|
The mean (expected value) and variance of the random variable are:
|
||||||
|
|
||||||
|
E[X] = (low + high + mode) / 3
|
||||||
|
Var[X] = (low**2 + high**2 + mode**2 - low*high - low*mode - high*mode) / 18
|
||||||
|
|
||||||
"""
|
"""
|
||||||
u = self.random()
|
u = self.random()
|
||||||
try:
|
try:
|
||||||
|
@ -593,12 +605,15 @@ class Random(_random.Random):
|
||||||
positive infinity if lambd is positive, and from negative
|
positive infinity if lambd is positive, and from negative
|
||||||
infinity to 0 if lambd is negative.
|
infinity to 0 if lambd is negative.
|
||||||
|
|
||||||
"""
|
The mean (expected value) and variance of the random variable are:
|
||||||
# lambd: rate lambd = 1/mean
|
|
||||||
# ('lambda' is a Python reserved word)
|
|
||||||
|
|
||||||
|
E[X] = 1 / lambd
|
||||||
|
Var[X] = 1 / lambd ** 2
|
||||||
|
|
||||||
|
"""
|
||||||
# we use 1-random() instead of random() to preclude the
|
# we use 1-random() instead of random() to preclude the
|
||||||
# possibility of taking the log of zero.
|
# possibility of taking the log of zero.
|
||||||
|
|
||||||
return -_log(1.0 - self.random()) / lambd
|
return -_log(1.0 - self.random()) / lambd
|
||||||
|
|
||||||
def vonmisesvariate(self, mu, kappa):
|
def vonmisesvariate(self, mu, kappa):
|
||||||
|
@ -654,8 +669,12 @@ class Random(_random.Random):
|
||||||
pdf(x) = --------------------------------------
|
pdf(x) = --------------------------------------
|
||||||
math.gamma(alpha) * beta ** alpha
|
math.gamma(alpha) * beta ** alpha
|
||||||
|
|
||||||
|
The mean (expected value) and variance of the random variable are:
|
||||||
|
|
||||||
|
E[X] = alpha * beta
|
||||||
|
Var[X] = alpha * beta ** 2
|
||||||
|
|
||||||
"""
|
"""
|
||||||
# alpha > 0, beta > 0, mean is alpha*beta, variance is alpha*beta**2
|
|
||||||
|
|
||||||
# Warning: a few older sources define the gamma distribution in terms
|
# Warning: a few older sources define the gamma distribution in terms
|
||||||
# of alpha > -1.0
|
# of alpha > -1.0
|
||||||
|
@ -714,6 +733,11 @@ class Random(_random.Random):
|
||||||
Conditions on the parameters are alpha > 0 and beta > 0.
|
Conditions on the parameters are alpha > 0 and beta > 0.
|
||||||
Returned values range between 0 and 1.
|
Returned values range between 0 and 1.
|
||||||
|
|
||||||
|
The mean (expected value) and variance of the random variable are:
|
||||||
|
|
||||||
|
E[X] = alpha / (alpha + beta)
|
||||||
|
Var[X] = alpha * beta / ((alpha + beta)**2 * (alpha + beta + 1))
|
||||||
|
|
||||||
"""
|
"""
|
||||||
## See
|
## See
|
||||||
## http://mail.python.org/pipermail/python-bugs-list/2001-January/003752.html
|
## http://mail.python.org/pipermail/python-bugs-list/2001-January/003752.html
|
||||||
|
@ -766,6 +790,11 @@ class Random(_random.Random):
|
||||||
|
|
||||||
Returns an integer in the range: 0 <= X <= n
|
Returns an integer in the range: 0 <= X <= n
|
||||||
|
|
||||||
|
The mean (expected value) and variance of the random variable are:
|
||||||
|
|
||||||
|
E[X] = n * p
|
||||||
|
Var[x] = n * p * (1 - p)
|
||||||
|
|
||||||
"""
|
"""
|
||||||
# Error check inputs and handle edge cases
|
# Error check inputs and handle edge cases
|
||||||
if n < 0:
|
if n < 0:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue