rand()函数在某种情况下,生成的随机数是一样的?

灼眼的超哥 2012-07-02 01:29:50
如题,看下面代码:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int test_time()
{
srand(time(NULL));
return rand()%10000;
}

int main()
{
printf("%d\n", test_time());
printf("%d\n", test_time());
printf("%d\n", test_time());
return 0;
}


三次输出的是一样的。

但改成下面这样,就正常了:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int test_time()
{
return rand()%10000;
}

int main()
{
srand(time(NULL));
printf("%d\n", test_time());
printf("%d\n", test_time());
printf("%d\n", test_time());
return 0;
}
...全文
395 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangdong20 2012-07-03
  • 打赏
  • 举报
回复
return rand()%10000;

这生成的是伪随机数,产生的随机数序列是一样的
srand(time(NULL));
return rand()%10000;

这里根据时间做为种子,因为每次时间是不同的,种子也就不同
所以这里产生的是真随机数
国外有个这个问题的典故
zhanshen2891 2012-07-02
  • 打赏
  • 举报
回复
它这个随机其实是一个随机序列,就是说当调用rand的时候值早已经确定好了,通过srand可以重置随机序列。
夏天__ 2012-07-02
  • 打赏
  • 举报
回复
随机数在某些场合(例如游戏程序)是非常有用的,但是用计算机生成完全随机的数却不是那么容易。计算机执行每一条指令的结果都是确定的,没有一条指令产生的是随机数,调用C标准库得到的随机数其实是伪随机(Pseudorandom)数,是用数学公式算出来的确定的数,只不过这些数看起来很随机,并且从统计意义上也很接近均匀分布(Uniform Distribution)的随机数。

C标准库中生成伪随机数的是rand函数,使用这个函数需要包含头文件stdlib.h,它没有参数,返回值是一个介于0和RAND_MAX之间的接近均匀分布的整数。RAND_MAX是该头文件中定义的一个常量,在不同的平台上有不同的取值,但可以肯定它是一个非常大的整数。通常我们用到的随机数是限定在某个范围之中的,例如0~9,而不是0~RAND_MAX,我们可以用%运算符将rand函数的返回值处理一下:

int x = rand() % 10;
灼眼的超哥 2012-07-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\rand.c
C/C++ code
/***
*rand.c - random number generator
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* ……
[/Quote]
如果是每次调用rand()前都调用srand(), ptd->_holdrand的值会在rand()修改它后又变回来?
赵4老师 2012-07-02
  • 打赏
  • 举报
回复
C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\rand.c
/***
*rand.c - random number generator
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines rand(), srand() - random number generator
*
*******************************************************************************/

#include <cruntime.h>
#include <mtdll.h>
#include <stddef.h>
#include <stdlib.h>

/***
*void srand(seed) - seed the random number generator
*
*Purpose:
* Seeds the random number generator with the int given. Adapted from the
* BASIC random number generator.
*
*Entry:
* unsigned seed - seed to seed rand # generator with
*
*Exit:
* None.
*
*Exceptions:
*
*******************************************************************************/

void __cdecl srand (
unsigned int seed
)
{
_getptd()->_holdrand = (unsigned long)seed;
}


/***
*int rand() - returns a random number
*
*Purpose:
* returns a pseudo-random number 0 through 32767.
*
*Entry:
* None.
*
*Exit:
* Returns a pseudo-random number 0 through 32767.
*
*Exceptions:
*
*******************************************************************************/

int __cdecl rand (
void
)
{
_ptiddata ptd = _getptd();

return( ((ptd->_holdrand = ptd->_holdrand * 214013L
+ 2531011L) >> 16) & 0x7fff );
}
灼眼的超哥 2012-07-02
  • 打赏
  • 举报
回复
如果每次生成随机数时,调用了srand()和rand(),那么rand()生成的随机数和上次一样。
如果在每次生成随机数前调用srand(),之后再多次调用rand(),生成的随机数就不一样了。

69,381

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧