Movatterモバイル変換


[0]ホーム

URL:


Random {base}R Documentation

Random Number Generation

Description

.Random.seed is an integer vector, containing the random numbergenerator (RNG)state for random number generation inR. Itcan be saved and restored, but should not be altered by the user.

RNGkind is a more friendly interface to query or set the kindof RNG in use.

RNGversion can be used to set the random generators as theywere in an earlierR version (for reproducibility).

set.seed is the recommended way to specify seeds.

Usage

.Random.seed <- c(rng.kind, n1, n2, ...)RNGkind(kind = NULL, normal.kind = NULL, sample.kind = NULL)RNGversion(vstr)set.seed(seed, kind = NULL, normal.kind = NULL, sample.kind = NULL)

Arguments

kind

character orNULL. Ifkind is a characterstring, setR's RNG to the kind desired. Use"default" toreturn to theR default. See ‘Details’ for theinterpretation ofNULL.

normal.kind

character string orNULL. If it is a characterstring, set the method of Normal generation. Use"default"to return to theR default.NULL makes no change.

sample.kind

character string orNULL. If it is a characterstring, set the method of discrete uniform generation (used insample, for instance). Use"default" to return to theR default.NULL makes no change.

seed

a single value, interpreted as an integer, orNULL(see ‘Details’).

vstr

a character string containing a version number,e.g.,"1.6.2". The default RNG configuration of the currentR version is used ifvstr is greater than the current version.

rng.kind

integer code in0:k for the abovekind.

n1,n2,...

integers. See the details for how many are required(which depends onrng.kind).

Details

The currently available RNG kinds are given below.kind ispartially matched to this list. The default is"Mersenne-Twister".

"Wichmann-Hill"

The seed,.Random.seed[-1] == r[1:3] is an integer vector oflength 3, where eachr[i] is in1:(p[i] - 1), wherep is the length 3 vector of primes,p = (30269, 30307, 30323).The Wichmann–Hill generator has a cycle length of6.9536 \times 10^{12} (=prod(p-1)/4, see⁠Wichmann and Hill (1984)which corrects the original article).It exhibits 12 clear failures in the TestU01 Crush suite and 22in the BigCrush suite (L'Ecuyer and Simard 2007).

"Marsaglia-Multicarry":

Amultiply-with-carry RNG is used, as recommended by GeorgeMarsaglia in his post to the mailing list ‘sci.stat.math’(Marsaglia 1997).It has a period of more than2^{60}.

It exhibits 40 clear failures in L'Ecuyer's TestU01 Crush suite.Combined with Ahrens-Dieter or Kinderman-Ramage it exhibitsdeviations from normality even for univariate distributiongeneration. SeePR#18168 for a discussion.

The seed is two integers (all values allowed).

"Super-Duper":

Marsaglia's famous Super-Duper from the 70's. This is the originalversion which doesnot pass the MTUPLE test of the Diehardbattery. It has a period of\approx 4.6\times 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, Hubert, and Abrahams (1982–4).

The two seeds are the Tausworthe and congruence long integers,respectively.

It exhibits 25 clear failures in the TestU01 Crush suite(L'Ecuyer and Simard 2007).

"Mersenne-Twister":

From ⁠Matsumoto and Nishimura (1998); code updated in 2002.A twistedGFSR with period2^{19937} - 1 and equidistribution in 623consecutive dimensions (over the whole period). The ‘seed’ is a624-dimensional set of 32-bit integers plus a current position inthat set.

R uses its own initialization method due to B. D. Ripley and isnot affected by the initialization issue in the 1998 code ofMatsumoto and Nishimura addressed in a 2002 update.

It exhibits 2 clear failures in each of the TestU01 Crush andthe BigCrush suite (L'Ecuyer and Simard 2007).

"Knuth-TAOCP-2002":

A 32-bit integerGFSR using lagged Fibonacci sequences withsubtraction. That is, the recurrence used is

X_j = (X_{j-100} - X_{j-37}) \bmod 2^{30}%

and the ‘seed’ is the set of the 100 last numbers (actuallyrecorded as 101 numbers, the last being a cyclic shift of thebuffer). The period is around2^{129}.

"Knuth-TAOCP":

An earlier version from ⁠Knuth (1997).

The 2002 version was not backwards compatible with the earlierversion: the initialization of theGFSR from the seed was altered.R did not allow you to choose consecutive seeds, the reported‘weakness’, and already scrambled the seeds. Otherwise,the algorithm is identical to Knuth-TAOCP-2002, with the samelagged Fibonacci recurrence formula.

Initialization of this generator is done in interpretedR codeand so takes a short but noticeable time.

It exhibits 3 clear failure in the TestU01 Crush suite and4 clear failures in the BigCrush suite(L'Ecuyer and Simard 2007).

"L'Ecuyer-CMRG":

A ‘combined multiple-recursive generator’ from⁠L'Ecuyer (1999),each element of which is a feedback multiplicativegenerator with three integer elements: thus the seed is a (signed)integer vector of length 6. The period is around2^{191}.

The 6 elements of the seed are internally regarded as 32-bitunsigned integers. Neither the first three nor the last threeshould be all zero, and they are limited to less than4294967087 and4294944443 respectively.

This is not particularly interesting of itself, but provides thebasis for the multiple streams used in packageparallel.

It exhibits 6 clear failures in each of the TestU01 Crush andthe BigCrush suite (L'Ecuyer and Simard 2007).

"user-supplied":

Use a user-supplied generator. SeeRandom.user fordetails.

normal.kind can be"Kinderman-Ramage","Buggy Kinderman-Ramage" (not forset.seed),"Ahrens-Dieter","Box-Muller","Inversion" (thedefault), or"user-supplied". (For inversion, see thereference inqnorm.) The Kinderman-Ramage generatorused in versions prior to 1.7.0 (now called"Buggy") had severalapproximation errors and should only be used for reproduction of oldresults. The"Box-Muller" generator is stateful as pairs ofnormals are generated and returned sequentially. The state is resetwhenever it is selected (even if it is the current normal generator)and whenkind is changed.

sample.kind can be"Rounding" or"Rejection",or partial matches to these. The former was the default in versionsprior to 3.6.0: it madesample noticeably non-uniformon large populations, and should only be used for reproduction of oldresults. SeePR#17494 for a discussion.

set.seed uses a 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"Mersenne-Twister" and"Knuth-TAOCP"). There is noguarantee that different values ofseed will seed the RNGdifferently, although any exceptions would be extremely rare. Ifcalled withseed = NULL it re-initializes (see ‘Note’)as if no seed had yet been set.

The use ofkind = NULL,normal.kind = NULL orsample.kind = NULL inRNGkind orset.seed selects the currently-usedgenerator (including that used in the previous session if theworkspace has been restored): if no generator has been used it selects"default".

Value

.Random.seed is aninteger vector whose firstelementcodes the kind of RNG and normal generator. The lowesttwo decimal digits are in0:(k-1)wherek is the number of availableRNGs. The hundredsrepresent the type of normal generator (starting at0), andthe ten thousands represent the type of discrete uniform sampler.

In the underlying C,.Random.seed[-1] isunsigned;therefore inR.Random.seed[-1] can be negative or perhapsNA_integer_, due tothe representation of an unsigned integer by a signed integer.

RNGkind returns a three-element character vector of the RNG,normal and sample kinds selectedbefore the call, invisibly if either argument is notNULL. A type starts a session as the default, and is selected either by a call toRNGkind or by setting.Random.seed in the workspace. (NB: prior toR 3.6.0 the firsttwo kinds were returned in a two-element character vector.)

RNGversion returns the same information asRNGkind aboutthe defaults in a specificR version.

set.seed returnsNULL, invisibly.

Note

Initially, there is no seed; a new one is created from the currenttime and the process ID when one is required. Hence differentsessions will give different simulation results, by default. However,the seed might be restored from a previous session if a previouslysaved workspace is restored.

.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, callset.seed (preferably withexplicit values forkind andnormal.kind) rather thanset.Random.seed.

The object.Random.seed is only looked for in the user'sworkspace.

Do not rely on randomness of low-order bits fromRNGs. Most of thesupplied uniform generators return 32-bit integer values that areconverted to doubles, so they take at most2^{32} distinctvalues and long runs will return duplicated values (Wichmann-Hill isthe exception, and all give at least 30 varying bits.)

Author(s)

of RNGkind: Martin Maechler. Current implementation, B. D. Ripleywith modifications by Duncan Murdoch.

References

Ahrens J. H., Dieter U. (1973).“Extensions of Forsythe's Method for Random Sampling from the Normal Distribution.”Mathematics of Computation,27, 927–937.doi:10.1090/S0025-5718-1973-0329190-8.

Becker R. A., Chambers J. M., Wilks A. R. (1988).The New S Language.Chapman and Hall/CRC, London.
(set.seed, storing in.Random.seed.)

Box G. E. P., Muller M. E. (1958).“A Note on the Generation of Random Normal Deviates.”The Annals of Mathematical Statistics,29(2), 610–611.doi:10.1214/aoms/1177706645.

De Matteis A., Pagnutti S. (1993).“Long-range Correlation Analysis of the Wichmann-Hill Random Number Generator.”Statistics and Computing,3(2), 67–70.doi:10.1007/BF00153065.

Kinderman A. J., Ramage J. G. (1976).“Computer Generation of Normal Random Variables.”Journal of the American Statistical Association,71(356), 893–896.doi:10.1080/01621459.1976.10480965.

Knuth D. E. (2002).The Art of Computer Programming, Volume 2: Seminumerical Algorithms, 3rd edition.Addison-Wesley Professional.Ninth printing.

Knuth D. E. (1997).The Art of Computer Programming, Volume 2: Seminumerical Algorithms, 3rd edition.Addison-Wesley Professional.
Source code athttps://www-cs-faculty.stanford.edu/~knuth/taocp.html.

L'Ecuyer P. (1999).“Good Parameters and Implementations for Combined Multiple Recursive Random Number Generators.”Operations Research,47(1), 159–164.doi:10.1287/opre.47.1.159.

L'Ecuyer P., Simard R. (2007).“TestU01: A C Library for Empirical Testing of Random Number Generators.”ACM Transactions on Mathematical Software,33(4), 1–40.doi:10.1145/1268776.1268777.
The TestU01 C library is available fromhttps://simul.iro.umontreal.ca/testu01/tu01.html or alsohttps://github.com/umontreal-simul/TestU01-2009.

Marsaglia G. (1997).“A Random Number Generator for C.”Discussion paper, posting on Usenet newsgroupsci.stat.math on September 29, 1997.

Marsaglia G., Zaman A. (1994).“Some Portable Very-long-period Random Number Generators.”Computers in Physics,8(1), 117–121.doi:10.1063/1.168514.

Matsumoto M., Nishimura T. (1998).“Mersenne Twister: A 623-dimensionally Equidistributed Uniform Pseudo-random Number Generator.”ACM Transactions on Modeling and Computer Simulation,8(1), 3–30.doi:10.1145/272991.272995.
Source code formerly athttp://www.math.keio.ac.jp/~matumoto/emt.html. Now seehttps://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/VERSIONS/C-LANG/c-lang.html.

Reeds J., Hubert S., Abrahams M. (1982–4).“C implementation of SuperDuper.”University of California at Berkeley. (Personal communication from Jim Reeds to Ross Ihaka.).

Wichmann B. A., Hill I. D. (1982).“Algorithm AS 183: An Efficient and Portable Pseudo-random Number Generator.”Applied Statistics,31(2), 188–190.doi:10.2307/2347988.Remarks:34, 198 and35, 89.

Wichmann B. A., Hill I. D. (1984).“Correction: Algorithm AS 183: An Efficient and Portable Pseudo-Random Number Generator.”Applied Statistics,33(1), 123.doi:10.2307/2347676.

See Also

sample for random sampling with and without replacement.

Distributions for functions for random-variate generation fromstandard distributions.

Examples

require(stats)## Seed the current RNG, i.e., set the RNG statusset.seed(42); u1 <- runif(30)set.seed(42); u2 <- runif(30) # the same because of identical RNG status:stopifnot(identical(u1, u2))## the default random seed is 626 integers, so only print a few runif(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]ok <- RNGkind()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 }set.seed(1998-12-04)# (when the next lines were added to the souRce)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.seedRNGkind("Super") # matches  "Super-Duper"RNGkind().Random.seed # new, corresponding to  Super-Duper## Reset:RNGkind(ok[1])RNGversion(getRversion()) # the default version for this R version## ----sum(duplicated(runif(1e6))) # around 110 for default generator## and we would expect about almost sure duplicates beyond aboutqbirthday(1 - 1e-6, classes = 2e9) # 235,000

[Packagebase version 4.6.0Index]

[8]ページ先頭

©2009-2025 Movatter.jp