Move binomialvariate() to a section for discrete distributions (GH-102955)

This commit is contained in:
Raymond Hettinger 2023-03-23 12:10:12 -05:00 committed by GitHub
parent f13fdacadf
commit 4695709143
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 23 deletions

View file

@ -404,8 +404,8 @@ Alternative Generator
Class that implements the default pseudo-random number generator used by the Class that implements the default pseudo-random number generator used by the
:mod:`random` module. :mod:`random` module.
.. deprecated:: 3.9 .. deprecated-removed:: 3.9 3.11
In the future, the *seed* must be one of the following types: Formerly the *seed* could be any hashable object. Now it is limited to:
:class:`NoneType`, :class:`int`, :class:`float`, :class:`str`, :class:`NoneType`, :class:`int`, :class:`float`, :class:`str`,
:class:`bytes`, or :class:`bytearray`. :class:`bytes`, or :class:`bytearray`.
@ -423,7 +423,7 @@ Notes on Reproducibility
------------------------ ------------------------
Sometimes it is useful to be able to reproduce the sequences given by a Sometimes it is useful to be able to reproduce the sequences given by a
pseudo-random number generator. By re-using a seed value, the same sequence should be pseudo-random number generator. By reusing a seed value, the same sequence should be
reproducible from run to run as long as multiple threads are not running. reproducible from run to run as long as multiple threads are not running.
Most of the random module's algorithms and seeding functions are subject to Most of the random module's algorithms and seeding functions are subject to

View file

@ -24,7 +24,6 @@
negative exponential negative exponential
gamma gamma
beta beta
binomial
pareto pareto
Weibull Weibull
@ -33,6 +32,11 @@
circular uniform circular uniform
von Mises von Mises
discrete distributions
----------------------
binomial
General notes on the underlying Mersenne Twister core generator: General notes on the underlying Mersenne Twister core generator:
* The period is 2**19937-1. * The period is 2**19937-1.
@ -731,6 +735,26 @@ class Random(_random.Random):
return y / (y + self.gammavariate(beta, 1.0)) return y / (y + self.gammavariate(beta, 1.0))
return 0.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): def binomialvariate(self, n=1, p=0.5):
"""Binomial random variable. """Binomial random variable.
@ -816,25 +840,6 @@ class Random(_random.Random):
return k 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 ------------------ ## --------------- Operating System Random Source ------------------