blob: 82a55e6fb6196798b174a2bd904a6f16f5e1874e [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
Tim Peters902446a2001-01-24 23:06:53 +000010distributions.
11For integers, uniform selection from a range.
12For sequences, uniform selection of a random element, and a function to
13generate a random permutation of a list in-place.
14On the real line, there are functions to compute uniform, normal (Gaussian),
15lognormal, negative exponential, gamma, and beta distributions.
16For generating distribution of angles, the circular uniform and
17von Mises distributions are available.
Guido van Rossum571391b1997-04-03 22:41:49 +000018
Tim Peters902446a2001-01-24 23:06:53 +000019Almost all module functions depend on the basic function
20\function{random()}, which generates a random float uniformly in
21the semi-open range [0.0, 1.0). Python uses the standard Wichmann-Hill
22generator, combining three pure multiplicative congruential
23generators of modulus 30269, 30307 and 30323. Its period (how many
24numbers it generates before repeating the sequence exactly) is
256,953,607,871,644. While of much higher quality than the \function{rand()}
26function supplied by most C libraries, the theoretical properties
27are much the same as for a single linear congruential generator of
28large modulus.
29
30The functions in this module are not threadsafe: if you want to call these
31functions from multiple threads, you should explicitly serialize the calls.
32Else, because no critical sections are implemented internally, calls
33from different threads may see the same return values.
34
35
36\begin{funcdesc}{seed}{\optional{x}}
37 Initialize the basic random number generator.
38 Optional argument \var{x} can be any hashable object,
39 and the generator is seeded from its hash code.
40 It is not guaranteed that distinct hash codes will produce distinct
41 seeds.
42 If \var{x} is omitted or \code{None},
43 the seed is derived from the current system time.
44 The seed is also set from the current system time when
45 the module is first imported.
Barry Warsaw83125772001-01-25 00:39:16 +000046\end{funcdesc}
Guido van Rossum571391b1997-04-03 22:41:49 +000047
Fred Drake5f0decf2001-01-22 18:18:30 +000048\begin{funcdesc}{choice}{seq}
Tim Peters902446a2001-01-24 23:06:53 +000049 Return a random element from the non-empty sequence \var{seq}.
Fred Drake5f0decf2001-01-22 18:18:30 +000050\end{funcdesc}
51
52\begin{funcdesc}{randint}{a, b}
53 \deprecated{2.0}{Use \function{randrange()} instead.}
Tim Peters902446a2001-01-24 23:06:53 +000054 Return a random integer \var{N} such that
Fred Drake5f0decf2001-01-22 18:18:30 +000055 \code{\var{a} <= \var{N} <= \var{b}}.
56\end{funcdesc}
57
Fred Drake5f0decf2001-01-22 18:18:30 +000058\begin{funcdesc}{randrange}{\optional{start,} stop\optional{, step}}
59 Return a randomly selected element from \code{range(\var{start},
60 \var{stop}, \var{step})}. This is equivalent to
Tim Peters902446a2001-01-24 23:06:53 +000061 \code{choice(range(\var{start}, \var{stop}, \var{step}))},
62 but doesn't actually build a range object.
Fred Drake5f0decf2001-01-22 18:18:30 +000063 \versionadded{1.5.2}
64\end{funcdesc}
65
Tim Peters902446a2001-01-24 23:06:53 +000066\begin{funcdesc}{random}{}
67 Return the next random floating point number in the range [0.0, 1.0).
68\end{funcdesc}
69
Fred Drake5f0decf2001-01-22 18:18:30 +000070\begin{funcdesc}{uniform}{a, b}
Tim Peters902446a2001-01-24 23:06:53 +000071 Return a random real number \var{N} such that
Fred Drake5f0decf2001-01-22 18:18:30 +000072 \code{\var{a} <= \var{N} < \var{b}}.
73\end{funcdesc}
Fred Drake38e5d272000-04-03 20:13:55 +000074
75
76The following functions are defined to support specific distributions,
77and all return real values. Function parameters are named after the
78corresponding variables in the distribution's equation, as used in
79common mathematical practice; most of these equations can be found in
Fred Drake5f0decf2001-01-22 18:18:30 +000080any statistics text.
81
Guido van Rossum571391b1997-04-03 22:41:49 +000082
Fred Drake2eda4ca1998-03-08 08:13:53 +000083\begin{funcdesc}{betavariate}{alpha, beta}
Fred Drake5f0decf2001-01-22 18:18:30 +000084 Beta distribution. Conditions on the parameters are
85 \code{\var{alpha} > -1} and \code{\var{beta} > -1}.
86 Returned values range between 0 and 1.
Guido van Rossum571391b1997-04-03 22:41:49 +000087\end{funcdesc}
88
Fred Drake2eda4ca1998-03-08 08:13:53 +000089\begin{funcdesc}{cunifvariate}{mean, arc}
Fred Drake5f0decf2001-01-22 18:18:30 +000090 Circular uniform distribution. \var{mean} is the mean angle, and
91 \var{arc} is the range of the distribution, centered around the mean
92 angle. Both values must be expressed in radians, and can range
Tim Peters902446a2001-01-24 23:06:53 +000093 between 0 and \emph{pi}. Returned values range between
Fred Drake5f0decf2001-01-22 18:18:30 +000094 \code{\var{mean} - \var{arc}/2} and \code{\var{mean} +
95 \var{arc}/2}.
Guido van Rossum571391b1997-04-03 22:41:49 +000096\end{funcdesc}
97
98\begin{funcdesc}{expovariate}{lambd}
Fred Drake5f0decf2001-01-22 18:18:30 +000099 Exponential distribution. \var{lambd} is 1.0 divided by the desired
100 mean. (The parameter would be called ``lambda'', but that is a
Tim Peters902446a2001-01-24 23:06:53 +0000101 reserved word in Python.) Returned values range from 0 to
Fred Drake5f0decf2001-01-22 18:18:30 +0000102 positive infinity.
Guido van Rossum571391b1997-04-03 22:41:49 +0000103\end{funcdesc}
104
Fred Drake2eda4ca1998-03-08 08:13:53 +0000105\begin{funcdesc}{gamma}{alpha, beta}
Fred Drake5f0decf2001-01-22 18:18:30 +0000106 Gamma distribution. (\emph{Not} the gamma function!) Conditions on
107 the parameters are \code{\var{alpha} > -1} and \code{\var{beta} > 0}.
Guido van Rossum571391b1997-04-03 22:41:49 +0000108\end{funcdesc}
109
Fred Drake2eda4ca1998-03-08 08:13:53 +0000110\begin{funcdesc}{gauss}{mu, sigma}
Fred Drake5f0decf2001-01-22 18:18:30 +0000111 Gaussian distribution. \var{mu} is the mean, and \var{sigma} is the
112 standard deviation. This is slightly faster than the
113 \function{normalvariate()} function defined below.
Guido van Rossum571391b1997-04-03 22:41:49 +0000114\end{funcdesc}
115
Fred Drake2eda4ca1998-03-08 08:13:53 +0000116\begin{funcdesc}{lognormvariate}{mu, sigma}
Fred Drake5f0decf2001-01-22 18:18:30 +0000117 Log normal distribution. If you take the natural logarithm of this
118 distribution, you'll get a normal distribution with mean \var{mu}
119 and standard deviation \var{sigma}. \var{mu} can have any value,
Tim Peters902446a2001-01-24 23:06:53 +0000120 and \var{sigma} must be greater than zero.
Guido van Rossum571391b1997-04-03 22:41:49 +0000121\end{funcdesc}
122
Fred Drake2eda4ca1998-03-08 08:13:53 +0000123\begin{funcdesc}{normalvariate}{mu, sigma}
Fred Drake5f0decf2001-01-22 18:18:30 +0000124 Normal distribution. \var{mu} is the mean, and \var{sigma} is the
125 standard deviation.
Guido van Rossum571391b1997-04-03 22:41:49 +0000126\end{funcdesc}
127
Fred Drake2eda4ca1998-03-08 08:13:53 +0000128\begin{funcdesc}{vonmisesvariate}{mu, kappa}
Fred Drake5f0decf2001-01-22 18:18:30 +0000129 \var{mu} is the mean angle, expressed in radians between 0 and
130 2*\emph{pi}, and \var{kappa} is the concentration parameter, which
131 must be greater than or equal to zero. If \var{kappa} is equal to
132 zero, this distribution reduces to a uniform random angle over the
133 range 0 to 2*\emph{pi}.
Guido van Rossum571391b1997-04-03 22:41:49 +0000134\end{funcdesc}
Guido van Rossume47da0a1997-07-17 16:34:52 +0000135
Guido van Rossum4f80b651997-12-30 17:38:05 +0000136\begin{funcdesc}{paretovariate}{alpha}
Fred Drake5f0decf2001-01-22 18:18:30 +0000137 Pareto distribution. \var{alpha} is the shape parameter.
Guido van Rossum4f80b651997-12-30 17:38:05 +0000138\end{funcdesc}
139
140\begin{funcdesc}{weibullvariate}{alpha, beta}
Fred Drake5f0decf2001-01-22 18:18:30 +0000141 Weibull distribution. \var{alpha} is the scale parameter and
142 \var{beta} is the shape parameter.
Guido van Rossum4f80b651997-12-30 17:38:05 +0000143\end{funcdesc}
Guido van Rossume47da0a1997-07-17 16:34:52 +0000144
Fred Drake065cba12000-12-15 19:07:17 +0000145
146This function does not represent a specific distribution, but
147implements a standard useful algorithm:
148
149\begin{funcdesc}{shuffle}{x\optional{, random}}
Fred Drake5f0decf2001-01-22 18:18:30 +0000150 Shuffle the sequence \var{x} in place.
151 The optional argument \var{random} is a 0-argument function
152 returning a random float in [0.0, 1.0); by default, this is the
153 function \function{random()}.
Fred Drake065cba12000-12-15 19:07:17 +0000154
Fred Drake5f0decf2001-01-22 18:18:30 +0000155 Note that for even rather small \code{len(\var{x})}, the total
156 number of permutations of \var{x} is larger than the period of most
157 random number generators; this implies that most permutations of a
158 long sequence can never be generated.
Fred Drake065cba12000-12-15 19:07:17 +0000159\end{funcdesc}
160
Guido van Rossume47da0a1997-07-17 16:34:52 +0000161\begin{seealso}
Tim Peters902446a2001-01-24 23:06:53 +0000162 \seetext{Wichmann, B. A. \& Hill, I. D., ``Algorithm AS 183:
163 An efficient and portable pseudo-random number generator'',
164 \citetitle{Applied Statistics} 31 (1982) 188-190.}
Guido van Rossume47da0a1997-07-17 16:34:52 +0000165\end{seealso}