blob: b76822e8ef1ac4feecb8151aee453ca95798f609 [file] [log] [blame]
Fred Drake3a0351c1998-04-04 07:23:21 +00001\section{Standard Module \module{random}}
Guido van Rossume47da0a1997-07-17 16:34:52 +00002\label{module-random}
Guido van Rossum571391b1997-04-03 22:41:49 +00003\stmodindex{random}
4
5This module implements pseudo-random number generators for various
6distributions: on the real line, there are functions to compute normal
7or Gaussian, lognormal, negative exponential, gamma, and beta
8distributions. For generating distribution of angles, the circular
9uniform and von Mises distributions are available.
10
11The module exports the following functions, which are exactly
Fred Drake2eda4ca1998-03-08 08:13:53 +000012equivalent to those in the \module{whrandom} module:
13\function{choice()}, \function{randint()}, \function{random()} and
14\function{uniform()}. See the documentation for the \module{whrandom}
15module for these functions.
Guido van Rossum571391b1997-04-03 22:41:49 +000016
Fred Drake2eda4ca1998-03-08 08:13:53 +000017The following functions specific to the \module{random} module are also
Guido van Rossum571391b1997-04-03 22:41:49 +000018defined, and all return real values. Function parameters are named
19after the corresponding variables in the distribution's equation, as
20used in common mathematical practice; most of these equations can be
21found in any statistics text.
22
Fred Drake2eda4ca1998-03-08 08:13:53 +000023\begin{funcdesc}{betavariate}{alpha, beta}
24Beta distribution. Conditions on the parameters are
Fred Drake4e668871998-04-03 06:04:12 +000025\code{\var{alpha} >- 1} and \code{\var{beta} > -1}.
Guido van Rossum571391b1997-04-03 22:41:49 +000026Returned values will range between 0 and 1.
27\end{funcdesc}
28
Fred Drake2eda4ca1998-03-08 08:13:53 +000029\begin{funcdesc}{cunifvariate}{mean, arc}
Guido van Rossum571391b1997-04-03 22:41:49 +000030Circular uniform distribution. \var{mean} is the mean angle, and
31\var{arc} is the range of the distribution, centered around the mean
32angle. Both values must be expressed in radians, and can range
Guido van Rossuma933f6a1998-04-20 14:43:44 +000033between 0 and pi. Returned values will range between
Fred Drake2eda4ca1998-03-08 08:13:53 +000034\code{\var{mean} - \var{arc}/2} and \code{\var{mean} + \var{arc}/2}.
Guido van Rossum571391b1997-04-03 22:41:49 +000035\end{funcdesc}
36
37\begin{funcdesc}{expovariate}{lambd}
Fred Drake2eda4ca1998-03-08 08:13:53 +000038Exponential distribution. \var{lambd} is 1.0 divided by the desired
39mean. (The parameter would be called ``lambda'', but that is a
40reserved word in Python.) Returned values will range from 0 to
41positive infinity.
Guido van Rossum571391b1997-04-03 22:41:49 +000042\end{funcdesc}
43
Fred Drake2eda4ca1998-03-08 08:13:53 +000044\begin{funcdesc}{gamma}{alpha, beta}
45Gamma distribution. (\emph{Not} the gamma function!) Conditions on
Fred Drake4e668871998-04-03 06:04:12 +000046the parameters are \code{\var{alpha} > -1} and \code{\var{beta} > 0}.
Guido van Rossum571391b1997-04-03 22:41:49 +000047\end{funcdesc}
48
Fred Drake2eda4ca1998-03-08 08:13:53 +000049\begin{funcdesc}{gauss}{mu, sigma}
Guido van Rossum571391b1997-04-03 22:41:49 +000050Gaussian distribution. \var{mu} is the mean, and \var{sigma} is the
51standard deviation. This is slightly faster than the
Fred Drake2eda4ca1998-03-08 08:13:53 +000052\function{normalvariate()} function defined below.
Guido van Rossum571391b1997-04-03 22:41:49 +000053\end{funcdesc}
54
Fred Drake2eda4ca1998-03-08 08:13:53 +000055\begin{funcdesc}{lognormvariate}{mu, sigma}
Guido van Rossum571391b1997-04-03 22:41:49 +000056Log normal distribution. If you take the natural logarithm of this
57distribution, you'll get a normal distribution with mean \var{mu} and
Fred Drake2eda4ca1998-03-08 08:13:53 +000058standard deviation \var{sigma}. \var{mu} can have any value, and \var{sigma}
Guido van Rossum571391b1997-04-03 22:41:49 +000059must be greater than zero.
60\end{funcdesc}
61
Fred Drake2eda4ca1998-03-08 08:13:53 +000062\begin{funcdesc}{normalvariate}{mu, sigma}
Guido van Rossum571391b1997-04-03 22:41:49 +000063Normal distribution. \var{mu} is the mean, and \var{sigma} is the
64standard deviation.
65\end{funcdesc}
66
Fred Drake2eda4ca1998-03-08 08:13:53 +000067\begin{funcdesc}{vonmisesvariate}{mu, kappa}
Guido van Rossuma933f6a1998-04-20 14:43:44 +000068\var{mu} is the mean angle, expressed in radians between 0 and 2*pi,
Guido van Rossum571391b1997-04-03 22:41:49 +000069and \var{kappa} is the concentration parameter, which must be greater
Guido van Rossuma933f6a1998-04-20 14:43:44 +000070than or equal to zero. If \var{kappa} is equal to zero, this
Guido van Rossum571391b1997-04-03 22:41:49 +000071distribution reduces to a uniform random angle over the range 0 to
Guido van Rossuma933f6a1998-04-20 14:43:44 +0000722*pi.
Guido van Rossum571391b1997-04-03 22:41:49 +000073\end{funcdesc}
Guido van Rossume47da0a1997-07-17 16:34:52 +000074
Guido van Rossum4f80b651997-12-30 17:38:05 +000075\begin{funcdesc}{paretovariate}{alpha}
76Pareto distribution. \var{alpha} is the shape parameter.
77\end{funcdesc}
78
79\begin{funcdesc}{weibullvariate}{alpha, beta}
Fred Drake2eda4ca1998-03-08 08:13:53 +000080Weibull distribution. \var{alpha} is the scale parameter and
Guido van Rossum4f80b651997-12-30 17:38:05 +000081\var{beta} is the shape parameter.
82\end{funcdesc}
Guido van Rossume47da0a1997-07-17 16:34:52 +000083
84\begin{seealso}
85\seemodule{whrandom}{the standard Python random number generator}
86\end{seealso}