blob: 54bca0f855c8ff64628a9f4fb5b0ab67f6112a67 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{random} ---
Fred Drake048b75b1999-04-21 18:14:22 +00002 Generate pseudo-random numbers}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drake048b75b1999-04-21 18:14:22 +00004\declaremodule{standard}{random}
Fred Drake295da241998-08-10 19:42:37 +00005\modulesynopsis{Generate pseudo-random numbers with various common
Fred Drake048b75b1999-04-21 18:14:22 +00006 distributions.}
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
Guido van Rossum571391b1997-04-03 22:41:49 +000015
Fred Drake38e5d272000-04-03 20:13:55 +000016The \module{random} module supports the \emph{Random Number
17Generator} interface, described in section \ref{rng-objects}. This
18interface of the module, as well as the distribution-specific
19functions described below, all use the pseudo-random generator
20provided by the \refmodule{whrandom} module.
21
22
23The following functions are defined to support specific distributions,
24and all return real values. Function parameters are named after the
25corresponding variables in the distribution's equation, as used in
26common mathematical practice; most of these equations can be found in
27any statistics text. These are expected to become part of the Random
28Number Generator interface in a future release.
Guido van Rossum571391b1997-04-03 22:41:49 +000029
Fred Drake2eda4ca1998-03-08 08:13:53 +000030\begin{funcdesc}{betavariate}{alpha, beta}
31Beta distribution. Conditions on the parameters are
Fred Drake38e5d272000-04-03 20:13:55 +000032\code{\var{alpha} > -1} and \code{\var{beta} > -1}.
33Returned values range between 0 and 1.
Guido van Rossum571391b1997-04-03 22:41:49 +000034\end{funcdesc}
35
Fred Drake2eda4ca1998-03-08 08:13:53 +000036\begin{funcdesc}{cunifvariate}{mean, arc}
Guido van Rossum571391b1997-04-03 22:41:49 +000037Circular uniform distribution. \var{mean} is the mean angle, and
38\var{arc} is the range of the distribution, centered around the mean
39angle. Both values must be expressed in radians, and can range
Fred Drake048b75b1999-04-21 18:14:22 +000040between 0 and \emph{pi}. Returned values will range between
Fred Drake2eda4ca1998-03-08 08:13:53 +000041\code{\var{mean} - \var{arc}/2} and \code{\var{mean} + \var{arc}/2}.
Guido van Rossum571391b1997-04-03 22:41:49 +000042\end{funcdesc}
43
44\begin{funcdesc}{expovariate}{lambd}
Fred Drake2eda4ca1998-03-08 08:13:53 +000045Exponential distribution. \var{lambd} is 1.0 divided by the desired
46mean. (The parameter would be called ``lambda'', but that is a
47reserved word in Python.) Returned values will range from 0 to
48positive infinity.
Guido van Rossum571391b1997-04-03 22:41:49 +000049\end{funcdesc}
50
Fred Drake2eda4ca1998-03-08 08:13:53 +000051\begin{funcdesc}{gamma}{alpha, beta}
52Gamma distribution. (\emph{Not} the gamma function!) Conditions on
Fred Drake4e668871998-04-03 06:04:12 +000053the parameters are \code{\var{alpha} > -1} and \code{\var{beta} > 0}.
Guido van Rossum571391b1997-04-03 22:41:49 +000054\end{funcdesc}
55
Fred Drake2eda4ca1998-03-08 08:13:53 +000056\begin{funcdesc}{gauss}{mu, sigma}
Guido van Rossum571391b1997-04-03 22:41:49 +000057Gaussian distribution. \var{mu} is the mean, and \var{sigma} is the
58standard deviation. This is slightly faster than the
Fred Drake2eda4ca1998-03-08 08:13:53 +000059\function{normalvariate()} function defined below.
Guido van Rossum571391b1997-04-03 22:41:49 +000060\end{funcdesc}
61
Fred Drake2eda4ca1998-03-08 08:13:53 +000062\begin{funcdesc}{lognormvariate}{mu, sigma}
Guido van Rossum571391b1997-04-03 22:41:49 +000063Log normal distribution. If you take the natural logarithm of this
64distribution, you'll get a normal distribution with mean \var{mu} and
Fred Drake38e5d272000-04-03 20:13:55 +000065standard deviation \var{sigma}. \var{mu} can have any value, and
66\var{sigma} must be greater than zero.
Guido van Rossum571391b1997-04-03 22:41:49 +000067\end{funcdesc}
68
Fred Drake2eda4ca1998-03-08 08:13:53 +000069\begin{funcdesc}{normalvariate}{mu, sigma}
Guido van Rossum571391b1997-04-03 22:41:49 +000070Normal distribution. \var{mu} is the mean, and \var{sigma} is the
71standard deviation.
72\end{funcdesc}
73
Fred Drake2eda4ca1998-03-08 08:13:53 +000074\begin{funcdesc}{vonmisesvariate}{mu, kappa}
Fred Drake048b75b1999-04-21 18:14:22 +000075\var{mu} is the mean angle, expressed in radians between 0 and 2*\emph{pi},
Guido van Rossum571391b1997-04-03 22:41:49 +000076and \var{kappa} is the concentration parameter, which must be greater
Guido van Rossuma933f6a1998-04-20 14:43:44 +000077than or equal to zero. If \var{kappa} is equal to zero, this
Guido van Rossum571391b1997-04-03 22:41:49 +000078distribution reduces to a uniform random angle over the range 0 to
Fred Drake048b75b1999-04-21 18:14:22 +0000792*\emph{pi}.
Guido van Rossum571391b1997-04-03 22:41:49 +000080\end{funcdesc}
Guido van Rossume47da0a1997-07-17 16:34:52 +000081
Guido van Rossum4f80b651997-12-30 17:38:05 +000082\begin{funcdesc}{paretovariate}{alpha}
83Pareto distribution. \var{alpha} is the shape parameter.
84\end{funcdesc}
85
86\begin{funcdesc}{weibullvariate}{alpha, beta}
Fred Drake2eda4ca1998-03-08 08:13:53 +000087Weibull distribution. \var{alpha} is the scale parameter and
Guido van Rossum4f80b651997-12-30 17:38:05 +000088\var{beta} is the shape parameter.
89\end{funcdesc}
Guido van Rossume47da0a1997-07-17 16:34:52 +000090
Fred Drake065cba12000-12-15 19:07:17 +000091
92This function does not represent a specific distribution, but
93implements a standard useful algorithm:
94
95\begin{funcdesc}{shuffle}{x\optional{, random}}
96Shuffle the sequence \var{x} in place.
97The optional argument \var{random} is a 0-argument function returning
98a random float in [0.0, 1.0); by default, this is the function
99\function{random()}.
100
101Note that for even rather small \code{len(\var{x})}, the total number
102of permutations of \var{x} is larger than the period of most random
103number generators; this implies that most permutations of a long
104sequence can never be generated.
105\end{funcdesc}
106
107
Guido van Rossume47da0a1997-07-17 16:34:52 +0000108\begin{seealso}
Fred Drake38e5d272000-04-03 20:13:55 +0000109 \seemodule{whrandom}{The standard Python random number generator.}
Guido van Rossume47da0a1997-07-17 16:34:52 +0000110\end{seealso}
Fred Drake38e5d272000-04-03 20:13:55 +0000111
112
113\subsection{The Random Number Generator Interface
114 \label{rng-objects}}
115
116% XXX This *must* be updated before a future release!
117
118The \dfn{Random Number Generator} interface describes the methods
119which are available for all random number generators. This will be
120enhanced in future releases of Python.
121
122In this release of Python, the modules \refmodule{random},
123\refmodule{whrandom}, and instances of the
124\class{whrandom.whrandom} class all conform to this interface.
125
126
127\begin{funcdesc}{choice}{seq}
128Chooses a random element from the non-empty sequence \var{seq} and
129returns it.
130\end{funcdesc}
131
132\begin{funcdesc}{randint}{a, b}
Fred Drakef0e88982000-06-30 15:32:31 +0000133\deprecated{2.0}{Use \function{randrange()} instead.}
Fred Drake38e5d272000-04-03 20:13:55 +0000134Returns a random integer \var{N} such that
135\code{\var{a} <= \var{N} <= \var{b}}.
136\end{funcdesc}
137
138\begin{funcdesc}{random}{}
139Returns the next random floating point number in the range [0.0
140... 1.0).
141\end{funcdesc}
142
Fred Drakef0e88982000-06-30 15:32:31 +0000143\begin{funcdesc}{randrange}{\optional{start,} stop\optional{, step}}
144Return a randomly selected element from \code{range(\var{start},
145\var{stop}, \var{step})}. This is equivalent to
146\code{choice(range(\var{start}, \var{stop}, \var{step}))}.
147\versionadded{1.5.2}
148\end{funcdesc}
149
Fred Drake38e5d272000-04-03 20:13:55 +0000150\begin{funcdesc}{uniform}{a, b}
151Returns a random real number \var{N} such that
152\code{\var{a} <= \var{N} < \var{b}}.
153\end{funcdesc}