问题描述
据我所知,rand()函数产生相同的号码每次运行它,如果你不改变种子数。这就是srand()函数的用武之地。时间总是在变化,所以我知道,你应该打发时间(空)参数函数srand。我的问题是与$ C $从下面的教程站点C。
I understand that rand() function generates the same number(s) each you run it if you don't change the seed number. That's where srand() comes in. Time is always changing so I know that you should pass the time(null) parameter to srand. My question is with the code below from a tutorial site.
int main()
{
int i, n=5;
time_t t;
/* Intializes random number generator */
srand((unsigned) time(&t));
/* Print 5 random numbers from 0 to 50 */
for( i = 0 ; i < n ; i++ ) {
printf("%d\n", rand() % 50);
}
return(0);
}
我看不出链接从函数srand
I see no link from the srand
((unsigned) time(&t));
和兰特。
printf("%d\n", rand() % 50);
在哪里兰特和函数srand之间的联系?我的意思还是想到的是我认为RAND()会得到一些srand函数参数(),所以它知道每次生成不同的号码。我相信它会看起来像兰特(函数srand(时间(空));
Where is the connection between rand and srand? What I mean or expect is I assume rand() will get some parameter from srand() so it knows to generate different numbers each time. I assume it would look something like rand(srand(time(null));
这就像初始化变量,而不使用它给我。函数srand正在初始化,但我没有看到它被使用。
It's like initializing a variable without using it to me. srand is being initialized, but I don't see it being used.
兰特是否产生不同的数字,因为在srand函数之前兰特先叫什么名字?
Does rand generate different numbers because srand is called first before rand?
推荐答案
随机数种子是一个全球性的静态变量。 兰特
和函数srand
都可以访问它。
The random number seed is a global static variable. rand
and srand
both have access to it.
这篇关于如何函数srand涉及到兰特功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!