Random
Random Number Generation
.Random.seed
is an integer vector, containing the random number
generator (RNG) state for random number generation in R. It
can be saved and restored, but should not be altered by the user. RNGkind
is a more friendly interface to query or set the kind
of RNG in use. RNGversion
can be used to set the random generators as they
were in an earlier R version (for reproducibility). set.seed
is the recommended way to specify seeds.
 Keywords
 distribution, sysdata
Usage
.Random.seed < c(rng.kind, n1, n2, \dots)
RNGkind(kind = NULL, normal.kind = NULL)
RNGversion(vstr)
set.seed(seed, kind = NULL, normal.kind = NULL)
Arguments
 kind
 character or
NULL
. Ifkind
is a character string, set R's RNG to the kind desired. Use"default"
to return to the R default. See ‘Details’ for the interpretation ofNULL
.  normal.kind
 character string or
NULL
. If it is a character string, set the method of Normal generation. Use"default"
to return to the R default.NULL
makes no change.  seed
 a single value, interpreted as an integer, or
NULL
(see ‘Details’).  vstr
 a character string containing a version number,
e.g.,
"1.6.2"
 rng.kind
 integer code in
0:k
for the abovekind
.  n1, n2, …
 integers. See the details for how many are required
(which depends on
rng.kind
).
Details
The currently available RNG kinds are given below. kind
is
partially matched to this list. The default is
"MersenneTwister"
.
"WichmannHill"

The seed,
.Random.seed[1] == r[1:3]
is an integer vector of length 3, where eachr[i]
is in1:(p[i]  1)
, wherep
is the length 3 vector of primes,p = (30269, 30307, 30323)
. The WichmannHill generator has a cycle length of \(6.9536 \times 10^{12}\) (=prod(p1)/4
, see Applied Statistics (1984) 33, 123 which corrects the original article). "MarsagliaMulticarry"
:
A multiplywithcarry RNG is used, as recommended by George
Marsaglia in his post to the mailing list
sci.stat.math
. It has a period of more than \(2^{60}\) and has passed all tests (according to Marsaglia). The seed is two integers (all values allowed). "SuperDuper"
:
Marsaglia's famous SuperDuper from the 70's. This is the original
version which does not pass the MTUPLE test of the Diehard
battery. It has a period of \(\approx 4.6\times 10^{18}\) for most initial seeds. The seed is two integers (all
values allowed for the first seed: the second must be odd). We use the implementation by Reeds et al (198284). The two seeds are the Tausworthe and congruence long integers,
respectively. A onetoone mapping to S's
.Random.seed[1:12]
is possible but we will not publish one, not least as this generator is not exactly the same as that in recent versions of SPLUS. "MersenneTwister"
: From Matsumoto and Nishimura (1998). A twisted GFSR with period \(2^{19937}  1\) and equidistribution in 623 consecutive dimensions (over the whole period). The ‘seed’ is a 624dimensional set of 32bit integers plus a current position in that set.
"KnuthTAOCP2002"
: A 32bit integer GFSR using lagged Fibonacci sequences with subtraction. That is, the recurrence used is $$X_j = (X_{j100}  X_{j37}) \bmod 2^{30}% $$ and the ‘seed’ is the set of the 100 last numbers (actually recorded as 101 numbers, the last being a cyclic shift of the buffer). The period is around \(2^{129}\).
"KnuthTAOCP"
: An earlier version from Knuth (1997). The 2002 version was not backwards compatible with the earlier version: the initialization of the GFSR from the seed was altered. R did not allow you to choose consecutive seeds, the reported ‘weakness’, and already scrambled the seeds. Initialization of this generator is done in interpreted R code and so takes a short but noticeable time.
"L'EcuyerCMRG"
:
A ‘combined multiplerecursive generator’ from L'Ecuyer
(1999), each element of which is a feedback multiplicative
generator with three integer elements: thus the seed is a (signed)
integer vector of length 6. The period is around
\(2^{191}\). The 6 elements of the seed are internally regarded as 32bit
unsigned integers. Neither the first three nor the last three
should be all zero, and they are limited to less than
4294967087
and4294944443
respectively. This is not particularly interesting of itself, but provides the basis for the multiple streams used in package parallel. "usersupplied"
:
Use a usersupplied generator. See
Random.user
for details.
normal.kind
can be "KindermanRamage"
,
"Buggy KindermanRamage"
(not for set.seed
),
"AhrensDieter"
, "BoxMuller"
, "Inversion"
(the
default), or "usersupplied"
. (For inversion, see the
reference in qnorm
.) The KindermanRamage generator
used in versions prior to 1.7.0 (now called "Buggy"
) had several
approximation errors and should only be used for reproduction of old
results. The "BoxMuller"
generator is stateful as pairs of
normals are generated and returned sequentially. The state is reset
whenever it is selected (even if it is the current normal generator)
and when kind
is changed. set.seed
uses a single integer argument to set as many seeds
as are required. It is intended as a simple way to get quite different
seeds by specifying small integer arguments, and also as a way to get
valid seed sets for the more complicated methods (especially
"MersenneTwister"
and "KnuthTAOCP"
). There is no
guarantee that different values of seed
will seed the RNG
differently, although any exceptions would be extremely rare. If
called with seed = NULL
it reinitializes (see ‘Note’)
as if no seed had yet been set. The use of kind = NULL
or normal.kind = NULL
in
RNGkind
or set.seed
selects the currentlyused
generator (including that used in the previous session if the
workspace has been restored): if no generator has been used it selects
"default"
.
Value
.Random.seed
is an integer
vector whose first
element codes the kind of RNG and normal generator. The lowest
two decimal digits are in 0:(k1)
where k
is the number of available RNGs. The hundreds
represent the type of normal generator (starting at 0
). In the underlying C, .Random.seed[1]
is unsigned
;
therefore in R .Random.seed[1]
can be negative, due to
the representation of an unsigned integer by a signed integer. RNGkind
returns a twoelement character vector of the RNG and
normal kinds selected before the call, invisibly if either
argument is not NULL
. A type starts a session as the default,
and is selected either by a call to RNGkind
or by setting
.Random.seed
in the workspace. RNGversion
returns the same information as RNGkind
about
the defaults in a specific R version. set.seed
returns NULL
, invisibly.
Note
Initially, there is no seed; a new one is created from the current
time and the process ID when one is required. Hence different
sessions will give different simulation results, by default. However,
the seed might be restored from a previous session if a previously
saved workspace is restored. .Random.seed
saves the seed set for the uniform randomnumber
generator, at least for the system generators. It does not
necessarily save the state of other generators, and in particular does
not save the state of the BoxMuller normal generator. If you want
to reproduce work later, call set.seed
(preferably with
explicit values for kind
and normal.kind
) rather than
set .Random.seed
. The object .Random.seed
is only looked for in the user's
workspace. Do not rely on randomness of loworder bits from RNGs. Most of the
supplied uniform generators return 32bit integer values that are
converted to doubles, so they take at most \(2^{32}\) distinct
values and long runs will return duplicated values (WichmannHill is
the exception, and all give at least 30 varying bits.)
References
Ahrens, J. H. and Dieter, U. (1973)
Extensions of Forsythe's method for random sampling from the normal
distribution.
Mathematics of Computation 27, 927937. Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
The New S Language.
Wadsworth & Brooks/Cole. (set.seed
, storing in .Random.seed
.) Box, G. E. P. and Muller, M. E. (1958) A note on the generation of normal
random deviates. Annals of Mathematical Statistics 29,
610611. De Matteis, A. and Pagnutti, S. (1993)
Longrange Correlation Analysis of the WichmannHill Random
Number Generator, Statist. Comput., 3, 6770. Kinderman, A. J. and Ramage, J. G. (1976)
Computer generation of normal random variables.
Journal of the American Statistical Association 71,
893896. Knuth, D. E. (1997)
The Art of Computer Programming. Volume 2, third edition.
Source code at http://wwwcsfaculty.stanford.edu/~knuth/taocp.html. Knuth, D. E. (2002)
The Art of Computer Programming. Volume 2, third edition, ninth
printing. L'Ecuyer, P. (1999) Good parameters and implementations for combined
multiple recursive random number generators. Operations
Research 47, 159164. Marsaglia, G. (1997) A random number generator for C. Discussion
paper, posting on Usenet newsgroup sci.stat.math
on
September 29, 1997. Marsaglia, G. and Zaman, A. (1994) Some portable verylongperiod
random number generators. Computers in Physics, 8,
117121. Matsumoto, M. and Nishimura, T. (1998)
Mersenne Twister: A 623dimensionally equidistributed uniform
pseudorandom number generator,
ACM Transactions on Modeling and Computer Simulation,
8, 330.
Source code formerly at http://www.math.keio.ac.jp/~matumoto/emt.html
.
Now see http://www.math.sci.hiroshimau.ac.jp/~mmat/MT/VERSIONS/CLANG/clang.html. Reeds, J., Hubert, S. and Abrahams, M. (19824) C implementation of
SuperDuper, University of California at Berkeley. (Personal
communication from Jim Reeds to Ross Ihaka.) Wichmann, B. A. and Hill, I. D. (1982)
Algorithm AS 183: An Efficient and Portable Pseudorandom Number
Generator, Applied Statistics, 31, 188190; Remarks:
34, 198 and 35, 89.
See Also
sample
for random sampling with and without replacement. Distributions for functions for randomvariate generation from
standard distributions.