Random(base)
Random()所属R语言包:base
Random Number Generation
随机数生成
译者:生物统计家园网 机器人LoveR
描述----------Description----------
.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.
.Random.seed是一个整数向量,包含R中生成随机数,它可以保存和恢复的随机数发生器(RNG)的状态,但不应该由用户改变。
RNGkind is a more friendly interface to query or set the kind of RNG in use.
RNGkind来查询或设置在使用RNG的是一个更加友好的界面。
RNGversion can be used to set the random generators as they were in an earlier R version (for reproducibility).
RNGversion可以用来设置随机发生器,因为他们在早期的R版本(重复性)。
set.seed is the recommended way to specify seeds.
set.seed是推荐的方式来指定种子。
用法----------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. If kind 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 of NULL.
字符或NULL。 kind如果是一个字符串,设置R的RNG的一种期望。使用"default"返回到R的默认。看到为NULL解释的“细节”。
参数: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.
字符串或NULL。如果它是一个字符串,设置正常发电的方法。使用"default"返回到R的默认。 NULL没有变化。
参数:seed
a single value, interpreted as an integer.
一个单一的价值,解释为一个整数。
参数:vstr
a character string containing a version number, e.g., "1.6.2"
一个字符串,包含一个版本号,例如,"1.6.2"
参数:rng.kind
integer code in 0:k for the above kind.
0:k上面的kind整数代码。
参数:n1, n2, ...
integers. See the details for how many are required (which depends on rng.kind).
整数。需要多少看到的细节(这取决于rng.kind)。
Details
详情----------Details----------
The currently available RNG kinds are given below. kind is partially matched to this list. The default is "Mersenne-Twister".
目前可用的RNG的各种情况如下。 kind部分匹配这个名单。默认"Mersenne-Twister"。
The seed, .Random.seed[-1] == r[1:3] is an integer vector of length 3, where each r[i] is in 1 p[i] - 1), where p is the length 3 vector of primes, p = (30269, 30307, 30323). The Wichmann–Hill generator has a cycle length of 6.9536e12 (= prod(p-1)/4, see Applied Statistics (1984)
种子,.Random.seed[-1] == r[1:3]是一个长度为3的整数向量,其中每个r[i]是在1 p[i] - 1),其中p是素数的长度为3向量,p = (30269, 30307, 30323) 。 Wichmann山发电机具有周期长,6.9536e12(prod(p-1)/4,看到应用统计(1984)
A multiply-with-carry 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
乘法与随身携带的RNG使用,建议在他的职位由乔治·马尔萨利亚邮件列表sci.stat.math。它有一个比2^60多的时期,并已通过了所有测试(根据马尔萨利亚)。种子是两个整数(所有
Marsaglia's famous Super-Duper 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 about 4.6*10^18 for most initial seeds. The seed is two integers (all values allowed for the first seed: the second must be odd).
马尔萨利亚著名的超级骗子从上世纪70年代。这是不通过MTUPLE测试的死硬电池的原始版本。它有一个about 4.6*10^18期间为最初始的种子。种子是两个整数(允许所有值的第一颗种子:第二,必须是奇数)。
We use the implementation by Reeds et al.\ (1982–84).
我们用芦苇等的实施。\(1982-84)。
The two seeds are the Tausworthe and congruence long integers, respectively. A one-to-one mapping to S's .Random.seed[1:12] is possible but we will not publish one, not least as this generator
两个种子的Tausworthe和长整数的一致性,分别。一个一对一映射到S的.Random.seed[1:12]是可能的,但我们不会公布这个发电机,至少
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 624-dimensional set of 32-bit integers plus a current position in that set.
从松本和西村(1998)。一个时期2^19937 - 1“等分布扭曲的”全球金融稳定报告“(整个期间)在连续623尺寸。 种子是一个32位整数的624维集,加那一套在当前位置。
A 32-bit integer GFSR using lagged Fibonacci sequences with subtraction. That is, the recurrence used is
一个32位整数的“全球金融稳定报告”,使用落后Fibonacci序列与减法。也就是说,使用复发
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.
和种子,是去年的100号码(实际上是101号码记录,最后一个缓冲区的循环移位)。这一时期是左右2^129。
An earlier version from Knuth (1997).
从克努特(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.
2002年的版本是不向后兼容早期版本:从种子期“全球金融稳定报告”的初始化被改变。 ŕ没有让你选择连续种子,报告的“缺点”,已经炒的种子。
Initialization of this generator is done in interpreted R code and so takes a short but noticeable time.
这种发电机的初始化是在解释R代码,所以需要很短,但显着的时间。
A "combined multiple-recursive 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.
A“组合多个递归发电机的,欧莱雅Ecuyer(1999),其中的每个元素是三个整数元素的反馈乘发电机:种子是一个长度为6的整数向量(签字)。这一时期是左右2^191。
The 6 elements of the seed are internally regarded as 32-bit unsigned integers. Neither the first three nor the last three should be all zero, and they are limited to less than 4294967087 and 4294944443 respectively.
国内种子的6种元素被视为32位无符号整数。无论是前三个,也不是最后三个应该是所有零,他们只限于比4294967087和4294944443分别少。
This is not particularly interesting of itself, but provides the basis for the multiple streams used in package parallel.
这本身不是特别有趣的,但提供包parallel使用多个数据流的基础。
Use a user-supplied generator. See Random.user for details.
使用用户提供的发电机。看到Random.user详情。
normal.kind can be "Kinderman-Ramage", "Buggy Kinderman-Ramage" (not for set.seed), "Ahrens-Dieter", "Box-Muller", "Inversion" (the default), or "user-supplied". (For inversion, see the reference in qnorm.) The Kinderman-Ramage generator used in versions prior to 1.7.1 (now called "Buggy") had several approximation errors and should only be used for reproduction of old results. The "Box-Muller" 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.
normal.kind能"Kinderman-Ramage","Buggy Kinderman-Ramage"(不set.seed)"Ahrens-Dieter","Box-Muller","Inversion"(默认),或"user-supplied"。 (反转,见参考qnorm。)的Kinderman Ramage发电机用于之前的版本1.7.1(现在称为"Buggy")有几个逼近误差,应仅用于再生产的老结果。 "Box-Muller"发生器的状态,对法线生成并返回的顺序。状态被重置时,它被选中(即使它是正常的电流发生器)和kind改变。
set.seed uses its 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 "Mersenne-Twister" and "Knuth-TAOCP"). There is no guarantee that different values of seed will seed the RNG differently, although any exceptions would be extremely rare.
set.seed使用其单个整数参数设置尽可能多的种子,需要。它的目的是作为一个简单的方式来获得完全不同的种子,由指定小的整数参数,尤其是作为一种更复杂的方法得到有效的种子集("Mersenne-Twister"和"Knuth-TAOCP")。有没有保证,不同的价值观seed将种子RNG的不同,虽然任何异常,将是极为罕见的。
The use of kind=NULL or normal.kind=NULL in RNGkind or set.seed selects the currently-used generator (including that used in the previous session if the workspace has been restored): if no generator has been used it selects "default".
使用kind=NULL或normal.kind=NULLRNGkind或set.seed选择目前使用的发电机(包括在上届会议已恢复使用的工作区):如果没有发电机已用它选择"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 k-1) where k is the number of available RNGs. The hundreds represent the type of normal generator (starting at 0).
.Random.seed是integer向量,其第一个元素的代码RNG和正常的发电机。最低的两位十进制数是在0 k-1)其中k是可用的RNG。数以百计的代表型正常发生器(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.
在底层C,.Random.seed[-1]是unsigned;R.Random.seed[-1]因此可以是负面的,由于有符号整数表示的无符号整数。
RNGkind returns a two-element 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.
RNGkind返回一个两个元素的特征向量,调用之前选择的RNG和正常种无形的,如果任一参数不是NULL。 A型为默认启动的会议,并通过电话或一个RNGkind设置在工作区中.Random.seed要么被选中。
RNGversion returns the same information as RNGkind about the defaults in a specific R version.
RNGversionRNGkind在一个特定的R版本默认返回相同的信息。
set.seed returns NULL, invisibly.
set.seedNULL,无形中返回。
注意----------Note----------
Initially, there is no seed; a new one is created from the current time (and since R 2.14.0, 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.
最初,没有种子,当一个人需要创建一个新的,从目前的时间(由于R 2.14.0,进程ID)。因此,不同的会议,会得出不同的模拟结果,默认情况下。然而,种子从以前的会议上可能会恢复,如果恢复以前保存的工作区。
.Random.seed saves the seed set for the uniform random-number 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 Box–Muller 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.
.Random.seed保存一套统一随机数发生器的种子,至少为系统发电机。它不一定保存其他发电机的状态,尤其是不保存的箱穆勒正常的发电机状态。如果你想复制工作后,调用set.seed,而不是集kind(明确normal.kind和.Random.seed值最好)。
The object .Random.seed is only looked for in the user's workspace.
对象.Random.seed只是看着在用户的工作空间。
Do not rely on randomness of low-order bits from RNGs. Most of the supplied uniform generators return 32-bit integer values that are converted to doubles, so they take at most 2^32 distinct values and long runs will return duplicated values (Wichmann-Hill is the exception, and all give at least 30 varying bits.)
不依赖于从低位的RNG的随机性。提供统一的发电机大部分返回的32位整数值转换到双打,所以他们在最2^32不同的价值观和长期运行,将返回重复值(Wichmann - 希尔是个例外,都给予至少30个不同的位)。
作者(S)----------Author(s)----------
of RNGkind: Martin Maechler. Current implementation, B. D. Ripley
参考文献----------References----------
Extensions of Forsythe's method for random sampling from the normal distribution. Mathematics of Computation 27, 927-937.
The New S Language. Wadsworth & Brooks/Cole. (<code>set.seed</code>, storing in <code>.Random.seed</code>.)
random deviates. Annals of Mathematical Statistics 29, 610–611.
Long-range Correlation Analysis of the Wichmann-Hill Random Number Generator, Statist. Comput., 3, 67–70.
Computer generation of normal random variables. Journal of the American Statistical Association 71, 893-896.
The Art of Computer Programming. Volume 2, third edition.<br> Source code at http://www-cs-faculty.stanford.edu/~knuth/taocp.html.
The Art of Computer Programming. Volume 2, third edition, ninth printing.<br> See http://Sunburn.Stanford.EDU/~knuth/news02.html.
multiple recursive random number generators. Operations Research 47, 159–164.
paper, posting on Usenet newsgroup <code>sci.stat.math</code> on September 29, 1997.
random number generators. Computers in Physics, 8, 117–121.
Mersenne Twister: A 623-dimensionally equidistributed uniform pseudo-random number generator, ACM Transactions on Modeling and Computer Simulation, 8, 3–30.<br> Source code at http://www.math.keio.ac.jp/~matumoto/emt.html.
SuperDuper, University of California at Berkeley. (Personal communication from Jim Reeds to Ross Ihaka.)
Algorithm AS 183: An Efficient and Portable Pseudo-random Number Generator, Applied Statistics, 31, 188–190; Remarks: 34, 198 and 35, 89.
参见----------See Also----------
sample for random sampling with and without replacement.
sample随机抽样与不更换。
Distributions for functions for random-variate generation from standard distributions.
分布为标准分布随机变量生成的功能。
举例----------Examples----------
require(stats)
## the default random seed is 626 integers, so only print a few[#默认的随机种子是626的整数,所以只有少数打印]
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,[这表明如何runif()。Wichmann - 希尔,]
## using only R functions:[#仅使用R函数:]
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.seed
RNGkind("Super")#matches "Super-Duper"[匹配“的骗子”]
RNGkind()
.Random.seed # new, corresponding to Super-Duper[新的,相应的骗子]
## Reset:[#复位:]
RNGkind(ok[1])
## ----[#----]
sum(duplicated(runif(1e6))) # around 110 for default generator[默认为大约110发电机]
## and we would expect about almost sure duplicates beyond about[#我们希望超越约几乎可以肯定重复]
qbirthday(1-1e-6, classes=2e9) # 235,000[235,000]
转载请注明:出自 生物统计家园网(http://www.biostatistic.net)。
注:
注1:为了方便大家学习,本文档为生物统计家园网机器人LoveR翻译而成,仅供个人R语言学习参考使用,生物统计家园保留版权。
注2:由于是机器人自动翻译,难免有不准确之处,使用时仔细对照中、英文内容进行反复理解,可以帮助R语言的学习。
注3:如遇到不准确之处,请在本贴的后面进行回帖,我们会逐渐进行修订。
|