Rev 25685 | Rev 27625 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
\name{Random}\alias{.Random.seed}\alias{RNG}\alias{RNGkind}\alias{RNGversion}\alias{set.seed}\title{Random Number Generation}\description{\code{.Random.seed} is an integer vector, containing the random numbergenerator (RNG) \bold{state} for random number generation in \R. Itcan be saved and restored, but should not be altered by the user.\code{RNGkind} is a more friendly interface to query or set the kindof RNG in use.\code{RNGversion} can be used to set the random generators as theywere in an earlier \R version (for reproducibility).\code{set.seed} is the recommended way to specify seeds.}\usage{.Random.seed <- c(rng.kind, n1, n2, \dots)save.seed <- .Random.seedRNGkind(kind = NULL, normal.kind = NULL)RNGversion(vstr)set.seed(seed, kind = NULL)}\arguments{\item{kind}{character or \code{NULL}. If \code{kind} is a characterstring, set \R's RNG to the kind desired. If it is \code{NULL},return the currently used RNG. Use \code{"default"} to return to the\R default.}\item{normal.kind}{character string or \code{NULL}. If it is a characterstring, set the method of Normal generation. Use \code{"default"}to return to the \R default.}\item{seed}{a single value, interpreted as an integer.}\item{vstr}{a character string containing a version number,e.g., \code{"1.6.2"}}\item{rng.kind}{integer code in \code{0:k} for the above \code{kind}.}\item{n1, n2, \dots}{integers. See the details for how many are required(which depends on \code{rng.kind}).}}\details{%% source and more detailed references: ../../../main/RNG.cThe currently available RNG kinds are given below. \code{kind} ispartially matched to this list. The default is\code{"Mersenne-Twister"}.\describe{\item{\code{"Wichmann-Hill"}}{The seed, \code{.Random.seed[-1] == r[1:3]} is an integer vector oflength 3, where each \code{r[i]} is in \code{1:(p[i] - 1)}, where\code{p} is the length 3 vector of primes, \code{p = (30269, 30307,30323)}.The Wichmann--Hill generator has a cycle length of\eqn{6.9536 \times 10^{12}}{6.9536e12} (=\code{prod(p-1)/4}, see \emph{Applied Statistics} (1984)\bold{33}, 123 which corrects the original article).}\item{\code{"Marsaglia-Multicarry"}:}{A \emph{multiply-with-carry} RNG is used, as recommended by GeorgeMarsaglia in his post to the mailing list \file{sci.stat.math}.It has a period of more than \eqn{2^{60}}{2^60} and has passedall tests (according to Marsaglia). The seed is two integers (allvalues allowed).}\item{\code{"Super-Duper"}:}{Marsaglia's famous Super-Duper from the 70's. This is the originalversion which does \emph{not} pass the MTUPLE test of the Diehardbattery. It has a period of \eqn{\approx 4.6\times 10^{18}}{about4.6*10^18} for most initial seeds. The seed is two integers (allvalues allowed for the first seed: the second must be odd).We use the implementation by Reeds et al.\ (1982--84).The two seeds are the Tausworthe and congruence long integers,respectively. A one-to-one mapping to S's \code{.Random.seed[1:12]}is possible but we will not publish one, not least as this generatoris \bold{not} exactly the same as that in recent versions of S-PLUS.}\item{\code{"Mersenne-Twister":}}{From Matsumoto and Nishimura (1998). A twisted GFSR with period\eqn{2^{19937} - 1}{2^19937 - 1} and equidistribution in 623consecutive dimensions (over the whole period). The \dQuote{seed} is a624-dimensional set of 32-bit integers plus a current position inthat set.}\item{\code{"Knuth-TAOCP":}}{From Knuth (1997). A GFSR using lagged Fibonacci sequences withsubtraction. That is, the recurrence used is\deqn{X_j = (X_{j-100} - X_{j-37}) \bmod 2^{30}%}{X[j] = (X[j-100] - X[j-37]) mod 2^30}and the \dQuote{seed} is the set of the 100 last numbers (actuallyrecorded as 101 numbers, the last being a cyclic shift of thebuffer). The period is around \eqn{2^{129}}{2^129}.}\item{\code{"Knuth-TAOCP-2002":}}{The 2002 version which not backwards compatible with the earlierversion: the initialization of the GFSR from the seed was altered.\R did not allow you to choose consecutiveseeds, the reported \sQuote{weakness}, and already scrambled the seeds.}\item{\code{"user-supplied":}}{Use a user-supplied generator. See \code{\link{Random.user}} for details.}}\code{normal.kind} can be \code{"Kinderman-Ramage"},\code{"Buggy Kinderman-Ramage"},\code{"Ahrens-Dieter"}, \code{"Box-Muller"}, \code{"Inversion"} (thedefault), or\code{"user-supplied"}. (For inversion, see the reference in\code{\link{qnorm}}.) The Kinderman-Ramage generator used in versions priorto 1.7.1 had several approximation errors and should only be used forreproduction of older results.\code{set.seed} uses its single integer argument to set as many seedsas are required. It is intended as a simple way to get quite differentseeds by specifying small integer arguments, and also as a way to getvalid seed sets for the more complicated methods (especially\code{"Mersenne-Twister"} and \code{"Knuth-TAOCP"}).}\note{Initially, there is no seed; a new one is created from the currenttime when one is required. Hence, different sessions will givedifferent simulation results, by default.\code{.Random.seed} saves the seed set for the uniform random-numbergenerator, at least for the system generators. It does notnecessarily save the state of other generators, and in particular doesnot save the state of the Box--Muller normal generator. If you wantto reproduce work later, call \code{set.seed} rather than set\code{.Random.seed}.As from \R 1.8.0, \code{.Random.seed} is only looked for in the user'sworkspace.}\value{\code{.Random.seed} is an \code{\link{integer}} vector whose firstelement \emph{codes} the kind of RNG and normal generator. The lowesttwo decimal digits are in \code{0:(k-1)}where \code{k} is the number of available RNGs. The hundredsrepresent the type of normal generator (starting at \code{0}).In the underlying C, \code{.Random.seed[-1]} is \code{unsigned};therefore in \R \code{.Random.seed[-1]} can be negative.\code{RNGkind} returns a two-element character vector of the RNG andnormal kinds in use \emph{before} the call, invisibly if eitherargument is not \code{NULL}. \code{RNGversion} returns the sameinformation.\code{set.seed} returns \code{NULL}, invisibly.}\references{Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)\emph{The New S Language}.Wadsworth \& Brooks/Cole. (\code{set.seed}, storing in \code{.Random.seed}.)Wichmann, B. A. and Hill, I. D. (1982)\emph{Algorithm AS 183: An Efficient and Portable Pseudo-random NumberGenerator}, Applied Statistics, \bold{31}, 188--190; Remarks:\bold{34}, 198 and \bold{35}, 89.De Matteis, A. and Pagnutti, S. (1993)\emph{Long-range Correlation Analysis of the Wichmann-Hill RandomNumber Generator}, Statist. Comput., \bold{3}, 67--70.Marsaglia, G. (1997) \emph{A random number generator for C.} Discussionpaper, posting on Usenet newsgroup \code{sci.stat.math} onSeptember 29, 1997.Reeds, J., Hubert, S. and Abrahams, M. (1982--4) C implementation ofSuperDuper, University of California at Berkeley. (Personalcommunication from Jim Reeds to Ross Ihaka.)Marsaglia, G. and Zaman, A. (1994) Some portable very-long-periodrandom number generators. \emph{Computers in Physics}, \bold{8},117--121.Matsumoto, M. and Nishimura, T. (1998)Mersenne Twister: A 623-dimensionally equidistributed uniformpseudo-random number generator,\emph{ACM Transactions on Modeling and Computer Simulation},\bold{8}, 3--30.\crSource code at \url{http://www.math.keio.ac.jp/~matumoto/emt.html}.Knuth, D. E. (1997)\emph{The Art of Computer Programming.} Volume 2, third edition.\crSource code at \url{http://www-cs-faculty.stanford.edu/~knuth/taocp.html}.Knuth, D. E. (2002)\emph{The Art of Computer Programming.} Volume 2, third edition, ninthprinting.\crSee \url{http://Sunburn.Stanford.EDU/~knuth/news02.html}.Kinderman, A. J. and Ramage, J. G. (1976)Computer generation of normal random variables.\emph{Journal of the American Statistical Association} \bold{71},893-896.Ahrens, J.H. and Dieter, U. (1973)Extensions of Forsythe's method for random sampling from the normaldistribution.\emph{Mathematics of Computation} \bold{27}, 927-937.Box, G.E.P. and Muller, M.E. (1958) A note on the generation of normalrandom deviates. \emph{Annals of Mathmatical Statistics} \bold{29},610--611.}\author{of RNGkind: Martin Maechler. Current implementation, B. D. Ripley}\seealso{\code{\link{runif}}, \code{\link{rnorm}}, \ldots.}% this is ./Uniform.Rd\examples{## the default random seed is 626 integers, so only print a fewrunif(1); .Random.seed[1:6]; runif(1); .Random.seed[1:6]## If there is no seed, a "random" new one is created:rm(.Random.seed); runif(1); .Random.seed[1:6]RNGkind("Wich")# (partial string matching on 'kind')## This shows how 'runif(.)' works for Wichmann-Hill,## using only R functions:p.WH <- c(30269, 30307, 30323)a.WH <- c( 171, 172, 170)next.WHseed <- function(i.seed = .Random.seed[-1]){ (a.WH * i.seed) \%\% p.WH }my.runif1 <- function(i.seed = .Random.seed){ ns <- next.WHseed(i.seed[-1]); sum(ns / p.WH) \%\% 1 }rs <- .Random.seed(WHs <- next.WHseed(rs[-1]))u <- runif(1)stopifnot(next.WHseed(rs[-1]) == .Random.seed[-1],all.equal(u, my.runif1(rs)))## ----.Random.seedok <- RNGkind()RNGkind("Super")#matches "Super-Duper"RNGkind().Random.seed # new, corresponding to Super-Duper## Reset:RNGkind(ok[1])}\keyword{distribution}\keyword{sysdata}