blob: c5df0ef7d06a905b5a04290a15937bafe854e464 [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 Drake269625f2001-02-03 01:12:44 +00007\deprecated{2.1}{Use \refmodule{random} instead.}
8
Fred Drake0aa811c2001-10-20 04:24:09 +00009\note{This module was an implementation detail of the
Fred Drake269625f2001-02-03 01:12:44 +000010\refmodule{random} module in releases of Python prior to 2.1. It is
11no longer used. Please do not use this module directly; use
Fred Drake0aa811c2001-10-20 04:24:09 +000012\refmodule{random} instead.}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000013
Guido van Rossum571391b1997-04-03 22:41:49 +000014This module implements a Wichmann-Hill pseudo-random number generator
Fred Drake38e5d272000-04-03 20:13:55 +000015class that is also named \class{whrandom}. Instances of the
16\class{whrandom} class conform to the Random Number Generator
17interface described in section \ref{rng-objects}. They also offer the
18following 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 Rossum571391b1997-04-03 22:41:49 +000031
32\begin{funcdesc}{choice}{seq}
33Chooses a random element from the non-empty sequence \var{seq} and returns it.
34\end{funcdesc}
35
Fred Drakecce10901998-03-17 06:33:25 +000036\begin{funcdesc}{randint}{a, b}
Guido van Rossum571391b1997-04-03 22:41:49 +000037Returns a random integer \var{N} such that \code{\var{a}<=\var{N}<=\var{b}}.
38\end{funcdesc}
39
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000040\begin{funcdesc}{random}{}
41Returns the next random floating point number in the range [0.0 ... 1.0).
42\end{funcdesc}
43
Fred Drakecce10901998-03-17 06:33:25 +000044\begin{funcdesc}{seed}{x, y, z}
Fred Drake37f15741999-11-10 16:21:37 +000045Initializes the random number generator from the integers \var{x},
46\var{y} and \var{z}. When the module is first imported, the random
47number is initialized using values derived from the current time.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000048\end{funcdesc}
Guido van Rossum571391b1997-04-03 22:41:49 +000049
Fred Drakecce10901998-03-17 06:33:25 +000050\begin{funcdesc}{uniform}{a, b}
Guido van Rossum571391b1997-04-03 22:41:49 +000051Returns a random real number \var{N} such that \code{\var{a}<=\var{N}<\var{b}}.
52\end{funcdesc}
53
Fred Drake38e5d272000-04-03 20:13:55 +000054When imported, the \module{whrandom} module also creates an instance of
55the \class{whrandom} class, and makes the methods of that instance
Guido van Rossum571391b1997-04-03 22:41:49 +000056available at the module level. Therefore one can write either
57\code{N = whrandom.random()} or:
Fred Drake38e5d272000-04-03 20:13:55 +000058
Fred Drake19479911998-02-13 06:58:54 +000059\begin{verbatim}
Guido van Rossum571391b1997-04-03 22:41:49 +000060generator = whrandom.whrandom()
61N = generator.random()
Fred Drake19479911998-02-13 06:58:54 +000062\end{verbatim}
Fred Drake37f15741999-11-10 16:21:37 +000063
Fred Drake38e5d272000-04-03 20:13:55 +000064Note that using separate instances of the generator leads to
65independent sequences of pseudo-random numbers.
66
Guido van Rossume47da0a1997-07-17 16:34:52 +000067\begin{seealso}
Fred Drake38e5d272000-04-03 20:13:55 +000068 \seemodule{random}{Generators for various random distributions and
69 documentation for the Random Number Generator
70 interface.}
Fred Drake37f15741999-11-10 16:21:37 +000071 \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 Rossume47da0a1997-07-17 16:34:52 +000074\end{seealso}