C 如果我没有';t指定srand(),rand()使用什么种子?

C 如果我没有';t指定srand(),rand()使用什么种子?,c,random,srand,C,Random,Srand,代码如下: #include <stdio.h> #include <stdlib.h> #include <time.h> int main(void) { int r; int i; for (i = 0; i < 100; i++) { r = rand() % 100 + 1; printf("%d\n", r); } return 0; } #包括 #包括 #包

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
    int r;
    int i;
    for (i = 0; i < 100; i++)
    {
        r = rand() % 100 + 1;
        printf("%d\n", r);
    }
    return 0;
}
#包括
#包括
#包括
内部主(空)
{
INTR;
int i;
对于(i=0;i<100;i++)
{
r=rand()%100+1;
printf(“%d\n”,r);
}
返回0;
}
我一直在尝试随机数,但有一天,我忘记将
srand()
放入,但是
rand()
函数仍然可以随机数(相同的序列)

问题是,如果我不指定它,它会使用什么种子

函数的作用是:将参数设置为新序列的种子 由rand()返回的伪随机整数的个数。这些序列 通过使用相同的种子值调用srand()可以重复

如果未提供种子值,则会自动调用rand()函数 以值1作为种子


如果未调用srand,rand的行为就好像调用了srand(1)


C标准实际上规定了其他答案中记录的行为:

ISO/IEC 9899:2011§7.22.2.2
srand
功能 ^2[…]如果在调用
srand
之前调用
rand
,则应生成与第一次调用
srand
时相同的序列,种子值为1

Ref:

函数srand()将其参数设置为rand()返回的新伪随机整数序列的种子。通过使用相同的种子值调用srand()可以重复这些序列


如果未提供种子值,rand()函数将自动使用值1作为种子。

欢迎使用堆栈溢出。你在这里所说的是正确的,但它在很大程度上重复了其他早期答案所说的。这一次我会给你一个质疑和投票的机会,但是当一个问题已经有很多答案,并且给出了与你将要添加的相同的信息时,你应该考虑是否应该添加你的答案。(当我添加我的答案时,已经有两个很好的答案;然而,我的答案添加了一个权威参考,说明函数为何会像它们那样运行——引用了C标准。这一独特的信息就是我添加答案的原因。)抱歉,我没有意识到答案已经存在。从现在起我不会再重复了。谢谢你指出错误。
If rand() is called before any calls to srand() are made, the same sequence shall 
be generated as when srand() is first called with a seed value of 1.