set.seed
Control Random Number Generator

### Description

Sets and inspects the state of the random number generator.

### Usage

    set.seed(seed, kind = NULL, normal.kind = NULL)
    RNGkind(kind = NULL, normal.kind = NULL)
    RNGversion(vstr)
    .Random.seed

### Arguments

seed | NULL or an integer value used to set the state of the random uniform generator. Each random uniform generator interprets the integer value in its own way. NULL means to choose a seed based on the time, a counter, and other information likely to produce a unique values.
kind | can be either a string or NULL (the default). If it is a string, it specifies the name of the desired random uniform generator. It must be one of "Wichmann-Hill", "Marsaglia-Multicarry", "Super-Duper", "Mersenne-Twister", "Knuth-TAOCP", "user-supplied", "Knuth-TAOCP-2002", "L'Ecuyer-CMRG" and "default", or an unambiguous abbreviation of one of those. Currently "default" is equivalent to "Mersenne-Twister". Currently, only the "Mersenne-Twister" and "L'Ecuyer-CMRG" generators are supported and all attempts to set another one result in a warning.
normal.kind | can be either a string or NULL (the default). If it is a string, it specifies the name of the desired random normal generator. It must be one of "Buggy Kinderman-Ramage", "Ahrens-Dieter", "Box-Muller", "user-supplied", "Inversion", "Kinderman-Ramage" and "default", or an unambiguous abbreviation of one of those. Currently "default" is equivalent to "Inversion". Currently, only the "Inversion" normal generator is supported and all attempts to set another one result in a warning.
vstr | a character string representing the R version number to be compatible with. Only the major and minor version numbers are used in RNGversion. Currently only version number 1.7 or later is supported.

### Details

- RNGkind changes the kind of the random uniform or normal generator and sets the seed to a time-based value. If neither kind nor normal.kind is provided, RNGkind does not reset or create a new seed; rather, it visibly returns the current kind and normal kind in a character vector.

  The dataset .Random.seed is used to store the current state of a random number generator. If it does not exist, it is created the first time a random number is generated. The RNG kind is set to "default" or to the last value used in the current session, and the seed is set to a time-based value. Essentially, .Random.seed is an integer vector with a variable length. The first element of .Random.seed records the kind and normal.kind information.

  Both kind and normal.kind strings can be mapped to zero-based indices: 0, 1, 2, ..., and then the first element of .Random.seed is set to the value 100*normal.kind_index + kind_index.

  The remaining elements of .Random.seed are the state of the chosen generator itself. Each generator uses .Random.seed in different ways and has its own requirements for the values in it, so it should not be modified. A user can store it and restore it to recreate a given stream of random numbers.

- set.seed sets the seed with the specified value and (optionally) changes the kind of random uniform or normal generator. The new seed value must be provided and valid.

- RNGversion calls RNGkind to set the kind of the random uniform or normal generator, according to the specified version. If the version number is 1.7 or later, the RNG uniform kind and normal kind are set as "Mersenne-Twister" and "Inversion".

Value

RNGkind | returns the previous kind and normal kind invisibly if at least one of kind or normal.kind is supplied with a new value. Returns the current kind and normal kind visibly if neither kind nor normal.kind is supplied.
set.seed | returns NULL invisibly.
RNGversion | returns the previous kind and normal kind invisibly.

Side Effects
set.seed, RNGkind, and RNGversion set the value of the .Random.seed object in the global environment. .Random.seed gives the state of the random number generation system.
Copyrights
M. Matsumoto and T. Nishimura generously made the Mersenne Twister code available for free use, even in commercial products. It was available under the GNU Public License until 2001, but the authors changed to a much more liberal licensing policy then. Their copyright notice follows A C-program for MT19937, with initialization improved 2002/1/26. Coded by Takuji Nishimura and Makoto Matsumoto.

Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. The names of its contributors may not be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Any feedback is very welcome. http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html email: m-mat \@ math.sci.hiroshima-u.ac.jp (remove space)

References
Ahrens, J. H. and Dieter, U. 1973. Extensions of Forsythe's method for random sampling from the normal distribution. Mathematics of Computation. Volume 27, 927-937.
Becker, R. A., Chambers, J. M., and Wilks, A. R. 1988. The New S Language. Pacific Grove, CA: Wadsworth & Brooks/Cole Advanced Books and Software. (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 Volume 29, 610--611.
De Matteis, A. and Pagnutti, S. 1993. Long-range Correlation Analysis of the Wichmann-Hill Random Number Generator. Statistical Computing Volume 3, 67--70.
Kennedy, W. J. and Gentle, J. E. 1980. Statistical Computing. New York, NY: Marcel Dekker.
Kinderman, A. J. and Ramage, J. G. 1976. Computer generation of normal random variables. Journal of the American Statistical Association Volume 71, 893-896.
Knuth, D. E. 1997. The Art of Computer Programming. Third Edition. (Source code at http://www-cs-faculty.stanford.edu/~knuth/taocp.html.)
Knuth, D. E. 2002. The Art of Computer Programming. Third Edition. Volume 2, Ninth printing. See http://Sunburn.Stanford.EDU/~knuth/news02.html.
Marsaglia, G., et al. 1973. Random Number Package: "Super-Duper". Montreal, Camada: McGill University School of Computer Science.
Marsaglia, G. and Zaman, A. 1994. Some portable very-long-period random number generators. Computers in Physics Volume 8 , 117-121.
Marsaglia, G. 1997. Discussion paper, posting on Usenet newsgroup sci.stat.math A random number generator for C.
Matsumoto, M. and Nishimura, T. 1988. Mersenne Twister: A 623-dimensionally equidistributed uniform pseudorandom number generator. ACM Trans. on Modeling and Computer Simulation Volume 8, No. 1, January 3-30. NOTE: A preprint of that paper can be found at http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/ARTICLES/mt.pdf. Source code at http://www.math.keio.ac.jp/~matumoto/emt.html.
Reeds, J., Hubert, S. and Abrahams, M. 1982-1984. Personal communication from Jim Reeds to Ross Ihaka. C implementation of SuperDuper, University of California at Berkeley.
R Development Core Team 2008. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing.
Venables, W. N. and Ripley, B. D. 1997. Modern Applied Statistics with S-PLUS Second Edition. New York, NY: Springer-Verlag.
Wichmann, B. A. and Hill, I. D. 1982. Algorithm AS 183: An Efficient and Portable Pseudo-random Number Generator Applied Statistics Volume 31 , 188-190; Remarks: Volume 34, 198 and Volume 35, 89.
See Also
rbeta, rcauchy, rchisq, rexp, rf, rgamma, rlogis, rlnorm, rnorm, rstab, rt, runif, rbinom, rgeom, rhyper, rpois, rwilcox, sample.
Examples

    # Make sure the new session is started!
    RNGkind()  # Returns "Mersenne-Twister" "Inversion"
    #.Random.seed   # Not created yet, so an error is generated
    # kind and normal.kind are set and .Random.seed is created.
    RNGkind("L'Ecuyer-CMRG", "default")
    .Random.seed   #  407 ... ... ....
    # kind = "Mersenne-Twister"  normal.kind = "Inversion"
    RNGversion("2.13.0")
    # to reproduce random samples, start with a specified random seed:
    set.seed(1, "Mersenne", "Inversion")
    runif(5)  # do some work
    set.seed(1, "Mersenne", "Inversion") # set the same seed again
    old.seed <- .Random.seed # store the seed
    runif(5)  # repeat work: the same "random" numbers occur.
    .Random.seed <<- old.seed # Reset .Random.seed in global environment
    runif(5)  # repeat work: the same "random" numbers occur.

Package base version 6.1.5-18
Package Index