blob: 48068762cfdce11a644d2df5346092ad8df0c1fb [file] [log] [blame]
Fred Drake3a0351c1998-04-04 07:23:21 +00001\section{Standard Module \module{random}}
Fred Drakeb91e9341998-07-23 17:59:49 +00002\declaremodule{standard}{random}
3
4\modulesynopsis{Generate pseudo-random numbers with various common distributions.}
5
Guido van Rossum571391b1997-04-03 22:41:49 +00006
7This module implements pseudo-random number generators for various
8distributions: on the real line, there are functions to compute normal
9or Gaussian, lognormal, negative exponential, gamma, and beta
10distributions. For generating distribution of angles, the circular
11uniform and von Mises distributions are available.
12
13The module exports the following functions, which are exactly
Fred Drake2eda4ca1998-03-08 08:13:53 +000014equivalent to those in the \module{whrandom} module:
15\function{choice()}, \function{randint()}, \function{random()} and
16\function{uniform()}. See the documentation for the \module{whrandom}
17module for these functions.
Guido van Rossum571391b1997-04-03 22:41:49 +000018
Fred Drake2eda4ca1998-03-08 08:13:53 +000019The following functions specific to the \module{random} module are also
Guido van Rossum571391b1997-04-03 22:41:49 +000020defined, and all return real values. Function parameters are named
21after the corresponding variables in the distribution's equation, as
22used in common mathematical practice; most of these equations can be
23found in any statistics text.
24
Fred Drake2eda4ca1998-03-08 08:13:53 +000025\begin{funcdesc}{betavariate}{alpha, beta}
26Beta distribution. Conditions on the parameters are
Fred Drake4e668871998-04-03 06:04:12 +000027\code{\var{alpha} >- 1} and \code{\var{beta} > -1}.
Guido van Rossum571391b1997-04-03 22:41:49 +000028Returned values will range between 0 and 1.
29\end{funcdesc}
30
Fred Drake2eda4ca1998-03-08 08:13:53 +000031\begin{funcdesc}{cunifvariate}{mean, arc}
Guido van Rossum571391b1997-04-03 22:41:49 +000032Circular uniform distribution. \var{mean} is the mean angle, and
33\var{arc} is the range of the distribution, centered around the mean
34angle. Both values must be expressed in radians, and can range
Guido van Rossuma933f6a1998-04-20 14:43:44 +000035between 0 and pi. Returned values will range between
Fred Drake2eda4ca1998-03-08 08:13:53 +000036\code{\var{mean} - \var{arc}/2} and \code{\var{mean} + \var{arc}/2}.
Guido van Rossum571391b1997-04-03 22:41:49 +000037\end{funcdesc}
38
39\begin{funcdesc}{expovariate}{lambd}
Fred Drake2eda4ca1998-03-08 08:13:53 +000040Exponential distribution. \var{lambd} is 1.0 divided by the desired
41mean. (The parameter would be called ``lambda'', but that is a
42reserved word in Python.) Returned values will range from 0 to
43positive infinity.
Guido van Rossum571391b1997-04-03 22:41:49 +000044\end{funcdesc}
45
Fred Drake2eda4ca1998-03-08 08:13:53 +000046\begin{funcdesc}{gamma}{alpha, beta}
47Gamma distribution. (\emph{Not} the gamma function!) Conditions on
Fred Drake4e668871998-04-03 06:04:12 +000048the parameters are \code{\var{alpha} > -1} and \code{\var{beta} > 0}.
Guido van Rossum571391b1997-04-03 22:41:49 +000049\end{funcdesc}
50
Fred Drake2eda4ca1998-03-08 08:13:53 +000051\begin{funcdesc}{gauss}{mu, sigma}
Guido van Rossum571391b1997-04-03 22:41:49 +000052Gaussian distribution. \var{mu} is the mean, and \var{sigma} is the
53standard deviation. This is slightly faster than the
Fred Drake2eda4ca1998-03-08 08:13:53 +000054\function{normalvariate()} function defined below.
Guido van Rossum571391b1997-04-03 22:41:49 +000055\end{funcdesc}
56
Fred Drake2eda4ca1998-03-08 08:13:53 +000057\begin{funcdesc}{lognormvariate}{mu, sigma}
Guido van Rossum571391b1997-04-03 22:41:49 +000058Log normal distribution. If you take the natural logarithm of this
59distribution, you'll get a normal distribution with mean \var{mu} and
Fred Drake2eda4ca1998-03-08 08:13:53 +000060standard deviation \var{sigma}. \var{mu} can have any value, and \var{sigma}
Guido van Rossum571391b1997-04-03 22:41:49 +000061must be greater than zero.
62\end{funcdesc}
63
Fred Drake2eda4ca1998-03-08 08:13:53 +000064\begin{funcdesc}{normalvariate}{mu, sigma}
Guido van Rossum571391b1997-04-03 22:41:49 +000065Normal distribution. \var{mu} is the mean, and \var{sigma} is the
66standard deviation.
67\end{funcdesc}
68
Fred Drake2eda4ca1998-03-08 08:13:53 +000069\begin{funcdesc}{vonmisesvariate}{mu, kappa}
Guido van Rossuma933f6a1998-04-20 14:43:44 +000070\var{mu} is the mean angle, expressed in radians between 0 and 2*pi,
Guido van Rossum571391b1997-04-03 22:41:49 +000071and \var{kappa} is the concentration parameter, which must be greater
Guido van Rossuma933f6a1998-04-20 14:43:44 +000072than or equal to zero. If \var{kappa} is equal to zero, this
Guido van Rossum571391b1997-04-03 22:41:49 +000073distribution reduces to a uniform random angle over the range 0 to
Guido van Rossuma933f6a1998-04-20 14:43:44 +0000742*pi.
Guido van Rossum571391b1997-04-03 22:41:49 +000075\end{funcdesc}
Guido van Rossume47da0a1997-07-17 16:34:52 +000076
Guido van Rossum4f80b651997-12-30 17:38:05 +000077\begin{funcdesc}{paretovariate}{alpha}
78Pareto distribution. \var{alpha} is the shape parameter.
79\end{funcdesc}
80
81\begin{funcdesc}{weibullvariate}{alpha, beta}
Fred Drake2eda4ca1998-03-08 08:13:53 +000082Weibull distribution. \var{alpha} is the scale parameter and
Guido van Rossum4f80b651997-12-30 17:38:05 +000083\var{beta} is the shape parameter.
84\end{funcdesc}
Guido van Rossume47da0a1997-07-17 16:34:52 +000085
86\begin{seealso}
87\seemodule{whrandom}{the standard Python random number generator}
88\end{seealso}