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