blob: cc00f198494d7133a4f94555ef699acf57126c29 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{whrandom} ---
Fred Drake38e5d272000-04-03 20:13:55 +00002 Pseudo-random number generator}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drake38e5d272000-04-03 20:13:55 +00004\declaremodule{standard}{whrandom}
Fred Drakeb91e9341998-07-23 17:59:49 +00005\modulesynopsis{Floating point pseudo-random number generator.}
6
Fred Drake68b64172001-02-02 02:41:17 +00007\strong{Note:} This module is an implementation detail of the
8\refmodule{random} module. Please do not use this module directly.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00009
Guido van Rossum571391b1997-04-03 22:41:49 +000010This module implements a Wichmann-Hill pseudo-random number generator
Fred Drake38e5d272000-04-03 20:13:55 +000011class that is also named \class{whrandom}. Instances of the
12\class{whrandom} class conform to the Random Number Generator
13interface described in section \ref{rng-objects}. They also offer the
14following 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 Rossum571391b1997-04-03 22:41:49 +000027
28\begin{funcdesc}{choice}{seq}
29Chooses a random element from the non-empty sequence \var{seq} and returns it.
30\end{funcdesc}
31
Fred Drakecce10901998-03-17 06:33:25 +000032\begin{funcdesc}{randint}{a, b}
Guido van Rossum571391b1997-04-03 22:41:49 +000033Returns a random integer \var{N} such that \code{\var{a}<=\var{N}<=\var{b}}.
34\end{funcdesc}
35
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000036\begin{funcdesc}{random}{}
37Returns the next random floating point number in the range [0.0 ... 1.0).
38\end{funcdesc}
39
Fred Drakecce10901998-03-17 06:33:25 +000040\begin{funcdesc}{seed}{x, y, z}
Fred Drake37f15741999-11-10 16:21:37 +000041Initializes the random number generator from the integers \var{x},
42\var{y} and \var{z}. When the module is first imported, the random
43number is initialized using values derived from the current time.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000044\end{funcdesc}
Guido van Rossum571391b1997-04-03 22:41:49 +000045
Fred Drakecce10901998-03-17 06:33:25 +000046\begin{funcdesc}{uniform}{a, b}
Guido van Rossum571391b1997-04-03 22:41:49 +000047Returns a random real number \var{N} such that \code{\var{a}<=\var{N}<\var{b}}.
48\end{funcdesc}
49
Fred Drake38e5d272000-04-03 20:13:55 +000050When imported, the \module{whrandom} module also creates an instance of
51the \class{whrandom} class, and makes the methods of that instance
Guido van Rossum571391b1997-04-03 22:41:49 +000052available at the module level. Therefore one can write either
53\code{N = whrandom.random()} or:
Fred Drake38e5d272000-04-03 20:13:55 +000054
Fred Drake19479911998-02-13 06:58:54 +000055\begin{verbatim}
Guido van Rossum571391b1997-04-03 22:41:49 +000056generator = whrandom.whrandom()
57N = generator.random()
Fred Drake19479911998-02-13 06:58:54 +000058\end{verbatim}
Fred Drake37f15741999-11-10 16:21:37 +000059
Fred Drake38e5d272000-04-03 20:13:55 +000060Note that using separate instances of the generator leads to
61independent sequences of pseudo-random numbers.
62
Guido van Rossume47da0a1997-07-17 16:34:52 +000063\begin{seealso}
Fred Drake38e5d272000-04-03 20:13:55 +000064 \seemodule{random}{Generators for various random distributions and
65 documentation for the Random Number Generator
66 interface.}
Fred Drake37f15741999-11-10 16:21:37 +000067 \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 Rossume47da0a1997-07-17 16:34:52 +000070\end{seealso}