问一个关于srand(time(0))和time(0)的问题

等等,还有一个bug 2019-05-18 08:08:55
如图
为啥两个会差别这么大呢?
...全文
421 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
独孤过 2019-05-21
  • 打赏
  • 举报
回复
引用 9 楼 等等,还有一个bug 的回复:
[quote=引用 1 楼 独孤由过 的回复:] Two different initializations with the same seed will generate the same succession of results in subsequent calls to rand. 在while循环中,由于程序执行快,使用time获取的系统时间,在一定时间间隔内相同,所以产生的随机数相同 srand在整个程序中只需被调用一次即可
我点成无满意结帖了,对不起啊啊啊[/quote] 没事
  • 打赏
  • 举报
回复
引用 7 楼 等等,还有一个bug 的回复:
[quote=引用 6 楼 赵4老师 的回复:] E:\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 );
}
谢谢!原来还可以这样学习[/quote]
引用 6 楼 赵4老师 的回复:
E:\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 );
}
我不小心点成无满意结帖了啊啊啊
  • 打赏
  • 举报
回复
引用 10 楼 636f6c696e 的回复:
你完全写反了,srand设置随机数种子,rand返回的是随机数,随机数根据随机数种子不同而不同。 [quote=引用 5 楼 等等,还有一个bug 的回复:] [quote=引用 4 楼 636f6c696e 的回复:] 多次调用rand返回结果当然不一样啊,不然还叫啥随机数生成接口。 只是如果设置同样的随机数种子,调用rand的结果和顺序是一致的,也就是第一次调用rand的结果都是xxx,第二次调用rand的结果都是yyy [quote=引用 3 楼 等等,还有一个bug 的回复:] [quote=引用 1 楼 独孤由过 的回复:] Two different initializations with the same seed will generate the same succession of results in subsequent calls to rand. 在while循环中,由于程序执行快,使用time获取的系统时间,在一定时间间隔内相同,所以产生的随机数相同 srand在整个程序中只需被调用一次即可
我发现time(0)貌似是一秒刷新一次,可是这样为什么第一种情况下还是持续得到不同的随机数呢?[/quote][/quote]哦,我懂了。意思是每次调用rand都会返回一个随机数种子,而调用srand的时候如果参数相同就会返回相同的随机树种子。[/quote][/quote]哦哦,意思是我第二张图就是每次循环设置了一次随机数种子,然后由于time()根据系统时间来计算的,所以有些时候srand就会设置相同的随机数种子。而第一张图只设置了一次,然后rand返回一个随机数的同时会改变随机数种子,所以能够得到不同的随机数。额,那个我不小心点成无满意结帖了,对不起啊啊啊。
636f6c696e 2019-05-20
  • 打赏
  • 举报
回复
你完全写反了,srand设置随机数种子,rand返回的是随机数,随机数根据随机数种子不同而不同。
引用 5 楼 等等,还有一个bug 的回复:
[quote=引用 4 楼 636f6c696e 的回复:] 多次调用rand返回结果当然不一样啊,不然还叫啥随机数生成接口。 只是如果设置同样的随机数种子,调用rand的结果和顺序是一致的,也就是第一次调用rand的结果都是xxx,第二次调用rand的结果都是yyy [quote=引用 3 楼 等等,还有一个bug 的回复:] [quote=引用 1 楼 独孤由过 的回复:] Two different initializations with the same seed will generate the same succession of results in subsequent calls to rand. 在while循环中,由于程序执行快,使用time获取的系统时间,在一定时间间隔内相同,所以产生的随机数相同 srand在整个程序中只需被调用一次即可
我发现time(0)貌似是一秒刷新一次,可是这样为什么第一种情况下还是持续得到不同的随机数呢?[/quote][/quote]哦,我懂了。意思是每次调用rand都会返回一个随机数种子,而调用srand的时候如果参数相同就会返回相同的随机树种子。[/quote]
  • 打赏
  • 举报
回复
引用 1 楼 独孤由过 的回复:
Two different initializations with the same seed will generate the same succession of results in subsequent calls to rand. 在while循环中,由于程序执行快,使用time获取的系统时间,在一定时间间隔内相同,所以产生的随机数相同 srand在整个程序中只需被调用一次即可
我点成无满意结帖了,对不起啊啊啊
  • 打赏
  • 举报
回复
不小心点成无满意结帖了,怎么改啊
  • 打赏
  • 举报
回复
引用 6 楼 赵4老师 的回复:
E:\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 );
}
谢谢!原来还可以这样学习
赵4老师 2019-05-19
  • 打赏
  • 举报
回复
E:\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 );
}
636f6c696e 2019-05-18
  • 打赏
  • 举报
回复
多次调用rand返回结果当然不一样啊,不然还叫啥随机数生成接口。 只是如果设置同样的随机数种子,调用rand的结果和顺序是一致的,也就是第一次调用rand的结果都是xxx,第二次调用rand的结果都是yyy
引用 3 楼 等等,还有一个bug 的回复:
[quote=引用 1 楼 独孤由过 的回复:] Two different initializations with the same seed will generate the same succession of results in subsequent calls to rand. 在while循环中,由于程序执行快,使用time获取的系统时间,在一定时间间隔内相同,所以产生的随机数相同 srand在整个程序中只需被调用一次即可
我发现time(0)貌似是一秒刷新一次,可是这样为什么第一种情况下还是持续得到不同的随机数呢?[/quote]
  • 打赏
  • 举报
回复
引用 1 楼 独孤由过 的回复:
Two different initializations with the same seed will generate the same succession of results in subsequent calls to rand. 在while循环中,由于程序执行快,使用time获取的系统时间,在一定时间间隔内相同,所以产生的随机数相同 srand在整个程序中只需被调用一次即可
我发现time(0)貌似是一秒刷新一次,可是这样为什么第一种情况下还是持续得到不同的随机数呢?
  • 打赏
  • 举报
回复
引用 1 楼 独孤由过 的回复:
Two different initializations with the same seed will generate the same succession of results in subsequent calls to rand. 在while循环中,由于程序执行快,使用time获取的系统时间,在一定时间间隔内相同,所以产生的随机数相同 srand在整个程序中只需被调用一次即可
意思是其实在第一个程序中每次随机数的seed是不同的,只是由于执行速度快的原因所以后面time(0)返回的数值没有变?可是这样的话srand为什么会在每次得到随机数后有不同的seed呢?
独孤过 2019-05-18
  • 打赏
  • 举报
回复
Two different initializations with the same seed will generate the same succession of results in subsequent calls to rand. 在while循环中,由于程序执行快,使用time获取的系统时间,在一定时间间隔内相同,所以产生的随机数相同 srand在整个程序中只需被调用一次即可
  • 打赏
  • 举报
回复
引用 4 楼 636f6c696e 的回复:
多次调用rand返回结果当然不一样啊,不然还叫啥随机数生成接口。 只是如果设置同样的随机数种子,调用rand的结果和顺序是一致的,也就是第一次调用rand的结果都是xxx,第二次调用rand的结果都是yyy [quote=引用 3 楼 等等,还有一个bug 的回复:] [quote=引用 1 楼 独孤由过 的回复:] Two different initializations with the same seed will generate the same succession of results in subsequent calls to rand. 在while循环中,由于程序执行快,使用time获取的系统时间,在一定时间间隔内相同,所以产生的随机数相同 srand在整个程序中只需被调用一次即可
我发现time(0)貌似是一秒刷新一次,可是这样为什么第一种情况下还是持续得到不同的随机数呢?[/quote][/quote]哦,我懂了。意思是每次调用rand都会返回一个随机数种子,而调用srand的时候如果参数相同就会返回相同的随机树种子。

65,206

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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