Fred Drake | 3a0351c | 1998-04-04 07:23:21 +0000 | [diff] [blame] | 1 | \section{Standard Module \module{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 | 2eda4ca | 1998-03-08 08:13:53 +0000 | [diff] [blame] | 23 | \begin{funcdesc}{betavariate}{alpha, beta} |
| 24 | Beta distribution. Conditions on the parameters are |
Fred Drake | 4e66887 | 1998-04-03 06:04:12 +0000 | [diff] [blame] | 25 | \code{\var{alpha} >- 1} and \code{\var{beta} > -1}. |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 26 | Returned values will range between 0 and 1. |
| 27 | \end{funcdesc} |
| 28 | |
Fred Drake | 2eda4ca | 1998-03-08 08:13:53 +0000 | [diff] [blame] | 29 | \begin{funcdesc}{cunifvariate}{mean, arc} |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 30 | Circular uniform distribution. \var{mean} is the mean angle, and |
| 31 | \var{arc} is the range of the distribution, centered around the mean |
| 32 | angle. Both values must be expressed in radians, and can range |
Fred Drake | 4e66887 | 1998-04-03 06:04:12 +0000 | [diff] [blame] | 33 | between 0 and $\pi$. Returned values will range between |
Fred Drake | 2eda4ca | 1998-03-08 08:13:53 +0000 | [diff] [blame] | 34 | \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] | 35 | \end{funcdesc} |
| 36 | |
| 37 | \begin{funcdesc}{expovariate}{lambd} |
Fred Drake | 2eda4ca | 1998-03-08 08:13:53 +0000 | [diff] [blame] | 38 | Exponential distribution. \var{lambd} is 1.0 divided by the desired |
| 39 | mean. (The parameter would be called ``lambda'', but that is a |
| 40 | reserved word in Python.) Returned values will range from 0 to |
| 41 | positive infinity. |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 42 | \end{funcdesc} |
| 43 | |
Fred Drake | 2eda4ca | 1998-03-08 08:13:53 +0000 | [diff] [blame] | 44 | \begin{funcdesc}{gamma}{alpha, beta} |
| 45 | Gamma distribution. (\emph{Not} the gamma function!) Conditions on |
Fred Drake | 4e66887 | 1998-04-03 06:04:12 +0000 | [diff] [blame] | 46 | 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] | 47 | \end{funcdesc} |
| 48 | |
Fred Drake | 2eda4ca | 1998-03-08 08:13:53 +0000 | [diff] [blame] | 49 | \begin{funcdesc}{gauss}{mu, sigma} |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 50 | Gaussian distribution. \var{mu} is the mean, and \var{sigma} is the |
| 51 | standard deviation. This is slightly faster than the |
Fred Drake | 2eda4ca | 1998-03-08 08:13:53 +0000 | [diff] [blame] | 52 | \function{normalvariate()} function defined below. |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 53 | \end{funcdesc} |
| 54 | |
Fred Drake | 2eda4ca | 1998-03-08 08:13:53 +0000 | [diff] [blame] | 55 | \begin{funcdesc}{lognormvariate}{mu, sigma} |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 56 | Log normal distribution. If you take the natural logarithm of this |
| 57 | distribution, you'll get a normal distribution with mean \var{mu} and |
Fred Drake | 2eda4ca | 1998-03-08 08:13:53 +0000 | [diff] [blame] | 58 | 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] | 59 | must be greater than zero. |
| 60 | \end{funcdesc} |
| 61 | |
Fred Drake | 2eda4ca | 1998-03-08 08:13:53 +0000 | [diff] [blame] | 62 | \begin{funcdesc}{normalvariate}{mu, sigma} |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 63 | Normal distribution. \var{mu} is the mean, and \var{sigma} is the |
| 64 | standard deviation. |
| 65 | \end{funcdesc} |
| 66 | |
Fred Drake | 2eda4ca | 1998-03-08 08:13:53 +0000 | [diff] [blame] | 67 | \begin{funcdesc}{vonmisesvariate}{mu, kappa} |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 68 | \var{mu} is the mean angle, expressed in radians between 0 and pi, |
| 69 | and \var{kappa} is the concentration parameter, which must be greater |
| 70 | then or equal to zero. If \var{kappa} is equal to zero, this |
| 71 | distribution reduces to a uniform random angle over the range 0 to |
Fred Drake | 2eda4ca | 1998-03-08 08:13:53 +0000 | [diff] [blame] | 72 | $2\pi$. |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 73 | \end{funcdesc} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 74 | |
Guido van Rossum | 4f80b65 | 1997-12-30 17:38:05 +0000 | [diff] [blame] | 75 | \begin{funcdesc}{paretovariate}{alpha} |
| 76 | Pareto distribution. \var{alpha} is the shape parameter. |
| 77 | \end{funcdesc} |
| 78 | |
| 79 | \begin{funcdesc}{weibullvariate}{alpha, beta} |
Fred Drake | 2eda4ca | 1998-03-08 08:13:53 +0000 | [diff] [blame] | 80 | Weibull distribution. \var{alpha} is the scale parameter and |
Guido van Rossum | 4f80b65 | 1997-12-30 17:38:05 +0000 | [diff] [blame] | 81 | \var{beta} is the shape parameter. |
| 82 | \end{funcdesc} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 83 | |
| 84 | \begin{seealso} |
| 85 | \seemodule{whrandom}{the standard Python random number generator} |
| 86 | \end{seealso} |