设置种子时的整数是什么意思? [英] What does the integer while setting the seed mean?

查看:143
本文介绍了设置种子时的整数是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 R 中的sample()函数从数据集中随机选择n行.每次获得不同的输出,因此使用set.seed()函数获得相同的输出.我知道set.seed()中的每个整数都会给我一个唯一的输出,如果设置相同的种子,则输出将是相同的.但是我无法确定作为参数传递给set.seed()函数的整数的含义.它只是进入随机数生成器算法的索引,还是意味着从您开始采样的那部分数据?例如,set.seed(2)中的2是什么意思?

I want to randomly select n rows from my data set using the sample() function in R. I was getting different outputs each time and hence used set.seed() function to get the same output. I know that each integer in the set.seed() will give me a unique output and the output will be the same if set the same seed. But I'm not able to make out what that integer that is passed as a parameter to the set.seed() function means. Is it just an index that goes into the random generator algorithm or does it mean some part of the data from where you start sampling? For example, what does the 2 in set.seed(2) mean?

推荐答案

随机种子(或种子状态,或仅种子)是用于初始化伪随机数生成器的数字(或向量).

A random seed (or seed state, or just seed) is a number (or vector) used to initialize a pseudorandom number generator.

对于要在伪随机数生成器中使用的种子,它不需要是随机的.由于数字生成算法的性质,只要忽略原始种子,该算法生成的其余值将以伪随机方式遵循概率分布.

For a seed to be used in a pseudorandom number generator, it does not need to be random. Because of the nature of number generating algorithms, so long as the original seed is ignored, the rest of the values that the algorithm generates will follow probability distribution in a pseudorandom manner.

-维基百科

因此,可以像这样实现随机函数:

So, random function could be implemented like this:

int rand_r(unsigned int *seed)
{
    *seed = *seed * 1103515245 + 12345;
    return (*seed % ((unsigned int)RAND_MAX + 1));
}

(取自glibc的示例)

(sample taken from glibc)

这篇关于设置种子时的整数是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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