c语言 srand,函数srand在C中

这段C++代码展示了如何使用`srand`和`rand`生成随机数。两次初始化`srand`相同的种子导致了`rand()`返回相同的序列。在代码中,首次调用`rand()`得到41,然后改变种子为当前时间,再次调用`rand()`得到不同的随机数。最后,重新设置种子为1,使得`rand()`再次返回41,证实了随机数生成器的可预测性。
摘要由CSDN通过智能技术生成

这段代码

#include

#include

#include

int main ()

{

printf ("First number: %d\n",rand() % 100);

srand ( time(NULL) );

printf ("Random number: %d\n",rand() % 100);

srand ( 1 );

printf ("Again the first number: %d\n",rand() %100);

return 0;

}

有以下输出:

First number: 41

Random number: 13

Again the first number: 41

还有以下规则:

Two different initializations with the same seed,instructs the pseudo-random generator to generate the same succession of results for the subsequent calls to rand in both cases.

我理解这些词,但我只是不理解方法本身.为什么它再次回归41?它是随机的还是必须根据此代码在每种情况下返回相同的结果?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值