Fred Drake | 3a0351c | 1998-04-04 07:23:21 +0000 | [diff] [blame] | 1 | \section{Standard Module \module{whrandom}} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 2 | \declaremodule{standard}{whrandom} |
| 3 | |
| 4 | \modulesynopsis{Floating point pseudo-random number generator.} |
| 5 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 7 | This module implements a Wichmann-Hill pseudo-random number generator |
| 8 | class that is also named \code{whrandom}. Instances of the |
| 9 | \code{whrandom} class have the following methods: |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 10 | |
| 11 | \begin{funcdesc}{choice}{seq} |
| 12 | Chooses a random element from the non-empty sequence \var{seq} and returns it. |
| 13 | \end{funcdesc} |
| 14 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 15 | \begin{funcdesc}{randint}{a, b} |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 16 | Returns a random integer \var{N} such that \code{\var{a}<=\var{N}<=\var{b}}. |
| 17 | \end{funcdesc} |
| 18 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 19 | \begin{funcdesc}{random}{} |
| 20 | Returns the next random floating point number in the range [0.0 ... 1.0). |
| 21 | \end{funcdesc} |
| 22 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 23 | \begin{funcdesc}{seed}{x, y, z} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 24 | Initializes the random number generator from the integers |
| 25 | \var{x}, |
| 26 | \var{y} |
| 27 | and |
| 28 | \var{z}. |
| 29 | When the module is first imported, the random number is initialized |
| 30 | using values derived from the current time. |
| 31 | \end{funcdesc} |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 32 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 33 | \begin{funcdesc}{uniform}{a, b} |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 34 | Returns a random real number \var{N} such that \code{\var{a}<=\var{N}<\var{b}}. |
| 35 | \end{funcdesc} |
| 36 | |
| 37 | When imported, the \code{whrandom} module also creates an instance of |
| 38 | the \code{whrandom} class, and makes the methods of that instance |
| 39 | available at the module level. Therefore one can write either |
| 40 | \code{N = whrandom.random()} or: |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 41 | \begin{verbatim} |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 42 | generator = whrandom.whrandom() |
| 43 | N = generator.random() |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 44 | \end{verbatim} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 45 | % |
| 46 | \begin{seealso} |
| 47 | \seemodule{random}{generators for various random distributions} |
| 48 | \seetext{Wichmann, B. A. \& Hill, I. D., ``Algorithm AS 183: |
| 49 | An efficient and portable pseudo-random number generator'', |
Fred Drake | cd14a87 | 1998-03-08 08:02:58 +0000 | [diff] [blame] | 50 | \emph{Applied Statistics} 31 (1982) 188-190} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 51 | \end{seealso} |