diff options
author | Raymond Hettinger <rhettinger@users.noreply.github.com> | 2023-03-23 17:10:12 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-23 17:10:12 (GMT) |
commit | 46957091433bfa097d7ea19b177bf42a52412f2d (patch) | |
tree | be12a93b9e276c38eae9b78a7dc4651b2e6f65e7 /Lib/random.py | |
parent | f13fdacadfca87c71130057b6f03c89ae640c9b2 (diff) | |
download | cpython-46957091433bfa097d7ea19b177bf42a52412f2d.zip cpython-46957091433bfa097d7ea19b177bf42a52412f2d.tar.gz cpython-46957091433bfa097d7ea19b177bf42a52412f2d.tar.bz2 |
Move binomialvariate() to a section for discrete distributions (GH-102955)
Diffstat (limited to 'Lib/random.py')
-rw-r--r-- | Lib/random.py | 45 |
1 files changed, 25 insertions, 20 deletions
diff --git a/Lib/random.py b/Lib/random.py index 3c4291f..586c3f7 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -24,7 +24,6 @@ negative exponential gamma beta - binomial pareto Weibull @@ -33,6 +32,11 @@ circular uniform von Mises + discrete distributions + ---------------------- + binomial + + General notes on the underlying Mersenne Twister core generator: * The period is 2**19937-1. @@ -731,6 +735,26 @@ class Random(_random.Random): return y / (y + self.gammavariate(beta, 1.0)) return 0.0 + def paretovariate(self, alpha): + """Pareto distribution. alpha is the shape parameter.""" + # Jain, pg. 495 + + u = 1.0 - self.random() + return u ** (-1.0 / alpha) + + def weibullvariate(self, alpha, beta): + """Weibull distribution. + + alpha is the scale parameter and beta is the shape parameter. + + """ + # Jain, pg. 499; bug fix courtesy Bill Arms + + u = 1.0 - self.random() + return alpha * (-_log(u)) ** (1.0 / beta) + + + ## -------------------- discrete distributions --------------------- def binomialvariate(self, n=1, p=0.5): """Binomial random variable. @@ -816,25 +840,6 @@ class Random(_random.Random): return k - def paretovariate(self, alpha): - """Pareto distribution. alpha is the shape parameter.""" - # Jain, pg. 495 - - u = 1.0 - self.random() - return u ** (-1.0 / alpha) - - def weibullvariate(self, alpha, beta): - """Weibull distribution. - - alpha is the scale parameter and beta is the shape parameter. - - """ - # Jain, pg. 499; bug fix courtesy Bill Arms - - u = 1.0 - self.random() - return alpha * (-_log(u)) ** (1.0 / beta) - - ## ------------------------------------------------------------------ ## --------------- Operating System Random Source ------------------ |