使用set.seed函数的原因 [英] Reasons for using the set.seed function

查看:88
本文介绍了使用set.seed函数的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很多时候,我在启动程序之前已经在R中看到了set.seed函数.我知道它基本上是用于随机数生成的.有什么特殊需要设置吗?

Many times I have seen the set.seed function in R, before starting the program. I know it's basically used for the random number generation. Is there any specific need to set this?

推荐答案

可能需要可重复的结果,例如,可能来自尝试调试程序,或者当然是来自尝试重做它的工作:

The need is the possible desire for reproducible results, which may for example come from trying to debug your program, or of course from trying to redo what it does:

当我只是要求随机"的东西时,我们将永远"复制这两个结果:

These two results we will "never" reproduce as I just asked for something "random":

R> sample(LETTERS, 5)
[1] "K" "N" "R" "Z" "G"
R> sample(LETTERS, 5)
[1] "L" "P" "J" "E" "D"

但是,这两个是相同的,因为我设置了种子:

These two, however, are identical because I set the seed:

R> set.seed(42); sample(LETTERS, 5)
[1] "X" "Z" "G" "T" "O"
R> set.seed(42); sample(LETTERS, 5)
[1] "X" "Z" "G" "T" "O"
R> 

关于所有这些都有大量的文献.维基百科是一个好的开始.本质上,这些RNG被称为伪随机数生成器,因为它们实际上是完全算法的:给定相同的种子,您将获得相同的序列.而且,是功能,而不是错误.

There is vast literature on all that; Wikipedia is a good start. In essence, these RNGs are called Pseudo Random Number Generators because they are in fact fully algorithmic: given the same seed, you get the same sequence. And that is a feature and not a bug.

这篇关于使用set.seed函数的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆