Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{whrandom} --- |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 2 | Pseudo-random number generator} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 3 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 4 | \declaremodule{standard}{whrandom} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 5 | \modulesynopsis{Floating point pseudo-random number generator.} |
| 6 | |
Fred Drake | 68b6417 | 2001-02-02 02:41:17 +0000 | [diff] [blame] | 7 | \strong{Note:} This module is an implementation detail of the |
| 8 | \refmodule{random} module. Please do not use this module directly. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 10 | This module implements a Wichmann-Hill pseudo-random number generator |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 11 | class that is also named \class{whrandom}. Instances of the |
| 12 | \class{whrandom} class conform to the Random Number Generator |
| 13 | interface described in section \ref{rng-objects}. They also offer the |
| 14 | following method, specific to the Wichmann-Hill algorithm: |
| 15 | |
| 16 | \begin{methoddesc}[whrandom]{seed}{\optional{x, y, z}} |
| 17 | Initializes the random number generator from the integers \var{x}, |
| 18 | \var{y} and \var{z}. When the module is first imported, the random |
| 19 | number is initialized using values derived from the current time. |
| 20 | If \var{x}, \var{y}, and \var{z} are either omitted or \code{0}, the |
| 21 | seed will be computed from the current system time. If one or two |
| 22 | of the parameters are \code{0}, but not all three, the zero values |
| 23 | are replaced by ones. This causes some apparently different seeds |
| 24 | to be equal, with the corresponding result on the pseudo-random |
| 25 | series produced by the generator. |
| 26 | \end{methoddesc} |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 27 | |
| 28 | \begin{funcdesc}{choice}{seq} |
| 29 | Chooses a random element from the non-empty sequence \var{seq} and returns it. |
| 30 | \end{funcdesc} |
| 31 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 32 | \begin{funcdesc}{randint}{a, b} |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 33 | Returns a random integer \var{N} such that \code{\var{a}<=\var{N}<=\var{b}}. |
| 34 | \end{funcdesc} |
| 35 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 36 | \begin{funcdesc}{random}{} |
| 37 | Returns the next random floating point number in the range [0.0 ... 1.0). |
| 38 | \end{funcdesc} |
| 39 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 40 | \begin{funcdesc}{seed}{x, y, z} |
Fred Drake | 37f1574 | 1999-11-10 16:21:37 +0000 | [diff] [blame] | 41 | Initializes the random number generator from the integers \var{x}, |
| 42 | \var{y} and \var{z}. When the module is first imported, the random |
| 43 | number is initialized using values derived from the current time. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 44 | \end{funcdesc} |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 45 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 46 | \begin{funcdesc}{uniform}{a, b} |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 47 | Returns a random real number \var{N} such that \code{\var{a}<=\var{N}<\var{b}}. |
| 48 | \end{funcdesc} |
| 49 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 50 | When imported, the \module{whrandom} module also creates an instance of |
| 51 | the \class{whrandom} class, and makes the methods of that instance |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 52 | available at the module level. Therefore one can write either |
| 53 | \code{N = whrandom.random()} or: |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 54 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 55 | \begin{verbatim} |
Guido van Rossum | 571391b | 1997-04-03 22:41:49 +0000 | [diff] [blame] | 56 | generator = whrandom.whrandom() |
| 57 | N = generator.random() |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 58 | \end{verbatim} |
Fred Drake | 37f1574 | 1999-11-10 16:21:37 +0000 | [diff] [blame] | 59 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 60 | Note that using separate instances of the generator leads to |
| 61 | independent sequences of pseudo-random numbers. |
| 62 | |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 63 | \begin{seealso} |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 64 | \seemodule{random}{Generators for various random distributions and |
| 65 | documentation for the Random Number Generator |
| 66 | interface.} |
Fred Drake | 37f1574 | 1999-11-10 16:21:37 +0000 | [diff] [blame] | 67 | \seetext{Wichmann, B. A. \& Hill, I. D., ``Algorithm AS 183: |
| 68 | An efficient and portable pseudo-random number generator'', |
| 69 | \citetitle{Applied Statistics} 31 (1982) 188-190.} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 70 | \end{seealso} |