Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 1 | \section{Standard Module \sectcode{random}} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 2 | \label{module-random} |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 3 | \stmodindex{random} |
| 4 | |
| 5 | This module implements pseudo-random number generators for various |
| 6 | distributions: on the real line, there are functions to compute normal |
| 7 | or Gaussian, lognormal, negative exponential, gamma, and beta |
| 8 | distributions. For generating distribution of angles, the circular |
| 9 | uniform and von Mises distributions are available. |
| 10 | |
| 11 | The module exports the following functions, which are exactly |
Fred Drake | 2eda4ca | 1998-03-08 08:13:53 +0000 | [diff] [blame] | 12 | equivalent 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} |
| 15 | module for these functions. |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 16 | |
Fred Drake | 2eda4ca | 1998-03-08 08:13:53 +0000 | [diff] [blame] | 17 | The following functions specific to the \module{random} module are also |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 18 | defined, and all return real values. Function parameters are named |
| 19 | after the corresponding variables in the distribution's equation, as |
| 20 | used in common mathematical practice; most of these equations can be |
| 21 | found in any statistics text. |
| 22 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 23 | \setindexsubitem{(in module random)} |
Fred Drake | 2eda4ca | 1998-03-08 08:13:53 +0000 | [diff] [blame] | 24 | \begin{funcdesc}{betavariate}{alpha, beta} |
| 25 | Beta distribution. Conditions on the parameters are |
Fred Drake | 4e66887 | 1998-04-03 06:04:12 +0000 | [diff] [blame] | 26 | \code{\var{alpha} >- 1} and \code{\var{beta} > -1}. |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 27 | Returned values will range between 0 and 1. |
| 28 | \end{funcdesc} |
| 29 | |
Fred Drake | 2eda4ca | 1998-03-08 08:13:53 +0000 | [diff] [blame] | 30 | \begin{funcdesc}{cunifvariate}{mean, arc} |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 31 | Circular uniform distribution. \var{mean} is the mean angle, and |
| 32 | \var{arc} is the range of the distribution, centered around the mean |
| 33 | angle. Both values must be expressed in radians, and can range |
Fred Drake | 4e66887 | 1998-04-03 06:04:12 +0000 | [diff] [blame] | 34 | between 0 and $\pi$. Returned values will range between |
Fred Drake | 2eda4ca | 1998-03-08 08:13:53 +0000 | [diff] [blame] | 35 | \code{\var{mean} - \var{arc}/2} and \code{\var{mean} + \var{arc}/2}. |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 36 | \end{funcdesc} |
| 37 | |
| 38 | \begin{funcdesc}{expovariate}{lambd} |
Fred Drake | 2eda4ca | 1998-03-08 08:13:53 +0000 | [diff] [blame] | 39 | Exponential distribution. \var{lambd} is 1.0 divided by the desired |
| 40 | mean. (The parameter would be called ``lambda'', but that is a |
| 41 | reserved word in Python.) Returned values will range from 0 to |
| 42 | positive infinity. |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 43 | \end{funcdesc} |
| 44 | |
Fred Drake | 2eda4ca | 1998-03-08 08:13:53 +0000 | [diff] [blame] | 45 | \begin{funcdesc}{gamma}{alpha, beta} |
| 46 | Gamma distribution. (\emph{Not} the gamma function!) Conditions on |
Fred Drake | 4e66887 | 1998-04-03 06:04:12 +0000 | [diff] [blame] | 47 | the parameters are \code{\var{alpha} > -1} and \code{\var{beta} > 0}. |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 48 | \end{funcdesc} |
| 49 | |
Fred Drake | 2eda4ca | 1998-03-08 08:13:53 +0000 | [diff] [blame] | 50 | \begin{funcdesc}{gauss}{mu, sigma} |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 51 | Gaussian distribution. \var{mu} is the mean, and \var{sigma} is the |
| 52 | standard deviation. This is slightly faster than the |
Fred Drake | 2eda4ca | 1998-03-08 08:13:53 +0000 | [diff] [blame] | 53 | \function{normalvariate()} function defined below. |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 54 | \end{funcdesc} |
| 55 | |
Fred Drake | 2eda4ca | 1998-03-08 08:13:53 +0000 | [diff] [blame] | 56 | \begin{funcdesc}{lognormvariate}{mu, sigma} |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 57 | Log normal distribution. If you take the natural logarithm of this |
| 58 | distribution, you'll get a normal distribution with mean \var{mu} and |
Fred Drake | 2eda4ca | 1998-03-08 08:13:53 +0000 | [diff] [blame] | 59 | standard deviation \var{sigma}. \var{mu} can have any value, and \var{sigma} |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 60 | must be greater than zero. |
| 61 | \end{funcdesc} |
| 62 | |
Fred Drake | 2eda4ca | 1998-03-08 08:13:53 +0000 | [diff] [blame] | 63 | \begin{funcdesc}{normalvariate}{mu, sigma} |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 64 | Normal distribution. \var{mu} is the mean, and \var{sigma} is the |
| 65 | standard deviation. |
| 66 | \end{funcdesc} |
| 67 | |
Fred Drake | 2eda4ca | 1998-03-08 08:13:53 +0000 | [diff] [blame] | 68 | \begin{funcdesc}{vonmisesvariate}{mu, kappa} |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 69 | \var{mu} is the mean angle, expressed in radians between 0 and pi, |
| 70 | and \var{kappa} is the concentration parameter, which must be greater |
| 71 | then or equal to zero. If \var{kappa} is equal to zero, this |
| 72 | distribution reduces to a uniform random angle over the range 0 to |
Fred Drake | 2eda4ca | 1998-03-08 08:13:53 +0000 | [diff] [blame] | 73 | $2\pi$. |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 74 | \end{funcdesc} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 75 | |
Guido van Rossum | 4f80b65 | 1997-12-30 17:38:05 +0000 | [diff] [blame] | 76 | \begin{funcdesc}{paretovariate}{alpha} |
| 77 | Pareto distribution. \var{alpha} is the shape parameter. |
| 78 | \end{funcdesc} |
| 79 | |
| 80 | \begin{funcdesc}{weibullvariate}{alpha, beta} |
Fred Drake | 2eda4ca | 1998-03-08 08:13:53 +0000 | [diff] [blame] | 81 | Weibull distribution. \var{alpha} is the scale parameter and |
Guido van Rossum | 4f80b65 | 1997-12-30 17:38:05 +0000 | [diff] [blame] | 82 | \var{beta} is the shape parameter. |
| 83 | \end{funcdesc} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 84 | |
| 85 | \begin{seealso} |
| 86 | \seemodule{whrandom}{the standard Python random number generator} |
| 87 | \end{seealso} |