blob: 63abbb38e8e2a11f5a7d48dff20ab68fa5713175 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{random} ---
2 Generate pseudo-random numbers with various distributions.}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{standard}{random}
4
Fred Drake295da241998-08-10 19:42:37 +00005\modulesynopsis{Generate pseudo-random numbers with various common
6distributions.}
Fred Drakeb91e9341998-07-23 17:59:49 +00007
Guido van Rossum571391b1997-04-03 22:41:49 +00008
9This module implements pseudo-random number generators for various
10distributions: on the real line, there are functions to compute normal
11or Gaussian, lognormal, negative exponential, gamma, and beta
12distributions. For generating distribution of angles, the circular
13uniform and von Mises distributions are available.
14
15The module exports the following functions, which are exactly
Fred Drake2eda4ca1998-03-08 08:13:53 +000016equivalent to those in the \module{whrandom} module:
17\function{choice()}, \function{randint()}, \function{random()} and
18\function{uniform()}. See the documentation for the \module{whrandom}
19module for these functions.
Guido van Rossum571391b1997-04-03 22:41:49 +000020
Fred Drake2eda4ca1998-03-08 08:13:53 +000021The following functions specific to the \module{random} module are also
Guido van Rossum571391b1997-04-03 22:41:49 +000022defined, and all return real values. Function parameters are named
23after the corresponding variables in the distribution's equation, as
24used in common mathematical practice; most of these equations can be
25found in any statistics text.
26
Fred Drake2eda4ca1998-03-08 08:13:53 +000027\begin{funcdesc}{betavariate}{alpha, beta}
28Beta distribution. Conditions on the parameters are
Fred Drake4e668871998-04-03 06:04:12 +000029\code{\var{alpha} >- 1} and \code{\var{beta} > -1}.
Guido van Rossum571391b1997-04-03 22:41:49 +000030Returned values will range between 0 and 1.
31\end{funcdesc}
32
Fred Drake2eda4ca1998-03-08 08:13:53 +000033\begin{funcdesc}{cunifvariate}{mean, arc}
Guido van Rossum571391b1997-04-03 22:41:49 +000034Circular uniform distribution. \var{mean} is the mean angle, and
35\var{arc} is the range of the distribution, centered around the mean
36angle. Both values must be expressed in radians, and can range
Guido van Rossuma933f6a1998-04-20 14:43:44 +000037between 0 and pi. Returned values will range between
Fred Drake2eda4ca1998-03-08 08:13:53 +000038\code{\var{mean} - \var{arc}/2} and \code{\var{mean} + \var{arc}/2}.
Guido van Rossum571391b1997-04-03 22:41:49 +000039\end{funcdesc}
40
41\begin{funcdesc}{expovariate}{lambd}
Fred Drake2eda4ca1998-03-08 08:13:53 +000042Exponential distribution. \var{lambd} is 1.0 divided by the desired
43mean. (The parameter would be called ``lambda'', but that is a
44reserved word in Python.) Returned values will range from 0 to
45positive infinity.
Guido van Rossum571391b1997-04-03 22:41:49 +000046\end{funcdesc}
47
Fred Drake2eda4ca1998-03-08 08:13:53 +000048\begin{funcdesc}{gamma}{alpha, beta}
49Gamma distribution. (\emph{Not} the gamma function!) Conditions on
Fred Drake4e668871998-04-03 06:04:12 +000050the parameters are \code{\var{alpha} > -1} and \code{\var{beta} > 0}.
Guido van Rossum571391b1997-04-03 22:41:49 +000051\end{funcdesc}
52
Fred Drake2eda4ca1998-03-08 08:13:53 +000053\begin{funcdesc}{gauss}{mu, sigma}
Guido van Rossum571391b1997-04-03 22:41:49 +000054Gaussian distribution. \var{mu} is the mean, and \var{sigma} is the
55standard deviation. This is slightly faster than the
Fred Drake2eda4ca1998-03-08 08:13:53 +000056\function{normalvariate()} function defined below.
Guido van Rossum571391b1997-04-03 22:41:49 +000057\end{funcdesc}
58
Fred Drake2eda4ca1998-03-08 08:13:53 +000059\begin{funcdesc}{lognormvariate}{mu, sigma}
Guido van Rossum571391b1997-04-03 22:41:49 +000060Log normal distribution. If you take the natural logarithm of this
61distribution, you'll get a normal distribution with mean \var{mu} and
Fred Drake2eda4ca1998-03-08 08:13:53 +000062standard deviation \var{sigma}. \var{mu} can have any value, and \var{sigma}
Guido van Rossum571391b1997-04-03 22:41:49 +000063must be greater than zero.
64\end{funcdesc}
65
Fred Drake2eda4ca1998-03-08 08:13:53 +000066\begin{funcdesc}{normalvariate}{mu, sigma}
Guido van Rossum571391b1997-04-03 22:41:49 +000067Normal distribution. \var{mu} is the mean, and \var{sigma} is the
68standard deviation.
69\end{funcdesc}
70
Fred Drake2eda4ca1998-03-08 08:13:53 +000071\begin{funcdesc}{vonmisesvariate}{mu, kappa}
Guido van Rossuma933f6a1998-04-20 14:43:44 +000072\var{mu} is the mean angle, expressed in radians between 0 and 2*pi,
Guido van Rossum571391b1997-04-03 22:41:49 +000073and \var{kappa} is the concentration parameter, which must be greater
Guido van Rossuma933f6a1998-04-20 14:43:44 +000074than or equal to zero. If \var{kappa} is equal to zero, this
Guido van Rossum571391b1997-04-03 22:41:49 +000075distribution reduces to a uniform random angle over the range 0 to
Guido van Rossuma933f6a1998-04-20 14:43:44 +0000762*pi.
Guido van Rossum571391b1997-04-03 22:41:49 +000077\end{funcdesc}
Guido van Rossume47da0a1997-07-17 16:34:52 +000078
Guido van Rossum4f80b651997-12-30 17:38:05 +000079\begin{funcdesc}{paretovariate}{alpha}
80Pareto distribution. \var{alpha} is the shape parameter.
81\end{funcdesc}
82
83\begin{funcdesc}{weibullvariate}{alpha, beta}
Fred Drake2eda4ca1998-03-08 08:13:53 +000084Weibull distribution. \var{alpha} is the scale parameter and
Guido van Rossum4f80b651997-12-30 17:38:05 +000085\var{beta} is the shape parameter.
86\end{funcdesc}
Guido van Rossume47da0a1997-07-17 16:34:52 +000087
88\begin{seealso}
89\seemodule{whrandom}{the standard Python random number generator}
90\end{seealso}