Skip to content

Commit b0e377f

Browse files
[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 19bf398) Co-authored-by: Raymond Hettinger <[email protected]>
1 parent b723b8a commit b0e377f

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

Lib/random.py

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,14 @@ def choices(self, population, weights=None, *, cum_weights=None, k=1):
492492
## -------------------- real-valued distributions -------------------
493493

494494
def uniform(self, a, b):
495-
"Get a random number in the range [a, b) or [a, b] depending on rounding."
495+
"""Get a random number in the range [a, b) or [a, b] depending on rounding.
496+
497+
The mean (expected value) and variance of the random variable are:
498+
499+
E[X] = (a + b) / 2
500+
Var[X] = (b - a) ** 2 / 12
501+
502+
"""
496503
return a + (b - a) * self.random()
497504

498505
def triangular(self, low=0.0, high=1.0, mode=None):
@@ -503,6 +510,11 @@ def triangular(self, low=0.0, high=1.0, mode=None):
503510
504511
http://en.wikipedia.org/wiki/Triangular_distribution
505512
513+
The mean (expected value) and variance of the random variable are:
514+
515+
E[X] = (low + high + mode) / 3
516+
Var[X] = (low**2 + high**2 + mode**2 - low*high - low*mode - high*mode) / 18
517+
506518
"""
507519
u = self.random()
508520
try:
@@ -593,12 +605,15 @@ def expovariate(self, lambd=1.0):
593605
positive infinity if lambd is positive, and from negative
594606
infinity to 0 if lambd is negative.
595607
596-
"""
597-
# lambd: rate lambd = 1/mean
598-
# ('lambda' is a Python reserved word)
608+
The mean (expected value) and variance of the random variable are:
609+
610+
E[X] = 1 / lambd
611+
Var[X] = 1 / lambd ** 2
599612
613+
"""
600614
# we use 1-random() instead of random() to preclude the
601615
# possibility of taking the log of zero.
616+
602617
return -_log(1.0 - self.random()) / lambd
603618

604619
def vonmisesvariate(self, mu, kappa):
@@ -654,8 +669,12 @@ def gammavariate(self, alpha, beta):
654669
pdf(x) = --------------------------------------
655670
math.gamma(alpha) * beta ** alpha
656671
672+
The mean (expected value) and variance of the random variable are:
673+
674+
E[X] = alpha * beta
675+
Var[X] = alpha * beta ** 2
676+
657677
"""
658-
# alpha > 0, beta > 0, mean is alpha*beta, variance is alpha*beta**2
659678

660679
# Warning: a few older sources define the gamma distribution in terms
661680
# of alpha > -1.0
@@ -714,6 +733,11 @@ def betavariate(self, alpha, beta):
714733
Conditions on the parameters are alpha > 0 and beta > 0.
715734
Returned values range between 0 and 1.
716735
736+
The mean (expected value) and variance of the random variable are:
737+
738+
E[X] = alpha / (alpha + beta)
739+
Var[X] = alpha * beta / ((alpha + beta)**2 * (alpha + beta + 1))
740+
717741
"""
718742
## See
719743
## http://mail.python.org/pipermail/python-bugs-list/2001-January/003752.html
@@ -766,6 +790,11 @@ def binomialvariate(self, n=1, p=0.5):
766790
767791
Returns an integer in the range: 0 <= X <= n
768792
793+
The mean (expected value) and variance of the random variable are:
794+
795+
E[X] = n * p
796+
Var[x] = n * p * (1 - p)
797+
769798
"""
770799
# Error check inputs and handle edge cases
771800
if n < 0:

0 commit comments

Comments
 (0)