Skip to content

More informative docstrings in the random module #109745

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions Lib/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,14 @@ def choices(self, population, weights=None, *, cum_weights=None, k=1):
## -------------------- real-valued distributions -------------------

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()

def triangular(self, low=0.0, high=1.0, mode=None):
Expand All @@ -503,6 +510,11 @@ def triangular(self, low=0.0, high=1.0, mode=None):

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()
try:
Expand Down Expand Up @@ -593,12 +605,15 @@ def expovariate(self, lambd=1.0):
positive infinity if lambd is positive, and from negative
infinity to 0 if lambd is negative.

"""
# lambd: rate lambd = 1/mean
# ('lambda' is a Python reserved word)
The mean (expected value) and variance of the random variable are:

E[X] = 1 / lambd
Var[X] = 1 / lambd ** 2

"""
# we use 1-random() instead of random() to preclude the
# possibility of taking the log of zero.

return -_log(1.0 - self.random()) / lambd

def vonmisesvariate(self, mu, kappa):
Expand Down Expand Up @@ -654,8 +669,12 @@ def gammavariate(self, alpha, beta):
pdf(x) = --------------------------------------
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
# of alpha > -1.0
Expand Down Expand Up @@ -714,6 +733,11 @@ def betavariate(self, alpha, beta):
Conditions on the parameters are alpha > 0 and beta > 0.
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
## http://mail.python.org/pipermail/python-bugs-list/2001-January/003752.html
Expand Down Expand Up @@ -766,6 +790,11 @@ def binomialvariate(self, n=1, p=0.5):

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
if n < 0:
Expand Down