gpt4 book ai didi

c++ - srand(时间(NULL));这个声明实际上在做什么

转载 作者:行者123 更新时间:2023-11-28 04:33:45 25 4
gpt4 key购买 nike

我只想知道这个 srand 函数是如何工作的

int Secret; // variable declaration
srand(time(NULL)); //calling srand function
Secret = rand() % 10 + 1; //generation random no between 0 and 10
cout<<Secret;

最佳答案

time() 获取当前时间http://www.cplusplus.com/reference/ctime/time

srand() 为您的 rand() 伪随机数生成器提供种子。

这是 C 风格的做事方式。 C++ 习惯用法是使用随机 header 及其函数。

请参阅 http://www.cplusplus.com/reference/random/generate_canonical/ 中的示例

迈克

关于c++ - srand(时间(NULL));这个声明实际上在做什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52129130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com