blob: b745f5bc055ac39121ec9a5b2a9f83b8154a1a81 [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
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00007
Guido van Rossum571391b1997-04-03 22:41:49 +00008This module implements a Wichmann-Hill pseudo-random number generator
Fred Drake38e5d272000-04-03 20:13:55 +00009class that is also named \class{whrandom}. Instances of the
10\class{whrandom} class conform to the Random Number Generator
11interface described in section \ref{rng-objects}. They also offer the
12following method, specific to the Wichmann-Hill algorithm:
13
14\begin{methoddesc}[whrandom]{seed}{\optional{x, y, z}}
15 Initializes the random number generator from the integers \var{x},
16 \var{y} and \var{z}. When the module is first imported, the random
17 number is initialized using values derived from the current time.
18 If \var{x}, \var{y}, and \var{z} are either omitted or \code{0}, the
19 seed will be computed from the current system time. If one or two
20 of the parameters are \code{0}, but not all three, the zero values
21 are replaced by ones. This causes some apparently different seeds
22 to be equal, with the corresponding result on the pseudo-random
23 series produced by the generator.
24\end{methoddesc}
Guido van Rossum571391b1997-04-03 22:41:49 +000025
26\begin{funcdesc}{choice}{seq}
27Chooses a random element from the non-empty sequence \var{seq} and returns it.
28\end{funcdesc}
29
Fred Drakecce10901998-03-17 06:33:25 +000030\begin{funcdesc}{randint}{a, b}
Guido van Rossum571391b1997-04-03 22:41:49 +000031Returns a random integer \var{N} such that \code{\var{a}<=\var{N}<=\var{b}}.
32\end{funcdesc}
33
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000034\begin{funcdesc}{random}{}
35Returns the next random floating point number in the range [0.0 ... 1.0).
36\end{funcdesc}
37
Fred Drakecce10901998-03-17 06:33:25 +000038\begin{funcdesc}{seed}{x, y, z}
Fred Drake37f15741999-11-10 16:21:37 +000039Initializes the random number generator from the integers \var{x},
40\var{y} and \var{z}. When the module is first imported, the random
41number is initialized using values derived from the current time.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000042\end{funcdesc}
Guido van Rossum571391b1997-04-03 22:41:49 +000043
Fred Drakecce10901998-03-17 06:33:25 +000044\begin{funcdesc}{uniform}{a, b}
Guido van Rossum571391b1997-04-03 22:41:49 +000045Returns a random real number \var{N} such that \code{\var{a}<=\var{N}<\var{b}}.
46\end{funcdesc}
47
Fred Drake38e5d272000-04-03 20:13:55 +000048When imported, the \module{whrandom} module also creates an instance of
49the \class{whrandom} class, and makes the methods of that instance
Guido van Rossum571391b1997-04-03 22:41:49 +000050available at the module level. Therefore one can write either
51\code{N = whrandom.random()} or:
Fred Drake38e5d272000-04-03 20:13:55 +000052
Fred Drake19479911998-02-13 06:58:54 +000053\begin{verbatim}
Guido van Rossum571391b1997-04-03 22:41:49 +000054generator = whrandom.whrandom()
55N = generator.random()
Fred Drake19479911998-02-13 06:58:54 +000056\end{verbatim}
Fred Drake37f15741999-11-10 16:21:37 +000057
Fred Drake38e5d272000-04-03 20:13:55 +000058Note that using separate instances of the generator leads to
59independent sequences of pseudo-random numbers.
60
Guido van Rossume47da0a1997-07-17 16:34:52 +000061\begin{seealso}
Fred Drake38e5d272000-04-03 20:13:55 +000062 \seemodule{random}{Generators for various random distributions and
63 documentation for the Random Number Generator
64 interface.}
Fred Drake37f15741999-11-10 16:21:37 +000065 \seetext{Wichmann, B. A. \& Hill, I. D., ``Algorithm AS 183:
66 An efficient and portable pseudo-random number generator'',
67 \citetitle{Applied Statistics} 31 (1982) 188-190.}
Guido van Rossume47da0a1997-07-17 16:34:52 +000068\end{seealso}