rand()函数有关问题

langhai2005 2011-04-12 12:34:33
MSDN中介绍The rand function returns a pseudorandom integer in the range 0 to RAND_MAX. Use the srand function to seed the pseudorandom-number generator before calling rand.
请问各位具体解释下RAND_MAX,与strand。谢谢。
...全文
445 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ulfsaar 2011-04-12
  • 打赏
  • 举报
回复
0 to RAND_MAX说的是产生的随机数范围是0~RAND_MAX,RAND_MAX应该是一个宏,具体值是多少可以查
strand可以当做是产生rand数之前的一个准备工作,你给该函数设一个值,rand函数就会根据这个值来产生随机数(一般都喜欢用系统时间)
pathuang68 2011-04-12
  • 打赏
  • 举报
回复
RAND_MAX就是rand函数所能产生的(伪)随机数的最大值。
srand和rand的关系:
The srand function sets the starting point for generating a series of pseudorandom integers in the current thread. To reinitialize the generator, use 1 as the seed argument. Any other value for seed sets the generator to a random starting point. rand retrieves the pseudorandom numbers that are generated. Calling rand before any call to srand generates the same sequence as calling srand with seed passed as 1.
langhai2005 2011-04-12
  • 打赏
  • 举报
回复
如果要产生一随机序列,比如长度为10,且要求其中的没一位数,都要求在1-10范围之间,要什么利用rand()实现,谢谢。[Quote=引用 6 楼 zhao4zhong1 的回复:]

引用 5 楼 chengzhe 的回复:
你看看rand的源码就知道了! 呵呵

c:\Microsoft SDK\src\crt\rand.c
C/C++ code
/***
*rand.c - random number generator
*
* Copyright (c) 1985-2001, Microsoft Corporation. All rights re……
[/Quote]
赵4老师 2011-04-12
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 chengzhe 的回复:]
你看看rand的源码就知道了! 呵呵
[/Quote]
c:\Microsoft SDK\src\crt\rand.c
/***
*rand.c - random number generator
*
* Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines rand(), srand() - random number generator
*
*******************************************************************************/

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

#ifndef _MT
static long holdrand = 1L;
#endif /* _MT */

/***
*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
)
{
#ifdef _MT

_getptd()->_holdrand = (unsigned long)seed;

#else /* _MT */
holdrand = (long)seed;
#endif /* _MT */
}


/***
*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
)
{
#ifdef _MT

_ptiddata ptd = _getptd();

return( ((ptd->_holdrand = ptd->_holdrand * 214013L
+ 2531011L) >> 16) & 0x7fff );

#else /* _MT */
return(((holdrand = holdrand * 214013L + 2531011L) >> 16) & 0x7fff);
#endif /* _MT */
}
chengzhe 2011-04-12
  • 打赏
  • 举报
回复
你看看rand的源码就知道了! 呵呵
delphiwcdj 2011-04-12
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 pathuang68 的回复:]

RAND_MAX就是rand函数所能产生的(伪)随机数的最大值。
srand和rand的关系:
The srand function sets the starting point for generating a series of pseudorandom integers in the current thread. To reinitialize the generator, use……
[/Quote]
UP
TypeCool 2011-04-12
  • 打赏
  • 举报
回复
RAND_MAX是VC中stdlib.h中宏定义的一个字符常量:
#define RAND_MAX 0x7FFF
根据系统环境的不同,其值最小可为32767,最大可为2147483647
这个常量指出当前rand()函数可以产生的最大随机数为多少.

strand()函数用来给rand()函数提供种子,如果种子相同的话,产生的随机数序列就会相同。
一般来说,会用srand((int)time(0));这样的调用来提供种子。
其中time(0)返回的是系统的时间(从1970.1.1午夜算起);单位:秒。
这样,只要不在同一秒内,就可产生不同序列的随机数了。

希望以上的简介对你有帮助。拿分、闪人。
langhai2005 2011-04-12
  • 打赏
  • 举报
回复
请问如何查找自己所需要函数的源代码,我安装的VC++06好像没有这个目录。[Quote=引用 6 楼 zhao4zhong1 的回复:]

引用 5 楼 chengzhe 的回复:
你看看rand的源码就知道了! 呵呵

c:\Microsoft SDK\src\crt\rand.c
C/C++ code
/***
*rand.c - random number generator
*
* Copyright (c) 1985-2001, Microsoft Corporation. All rights re……
[/Quote]
langhai2005 2011-04-12
  • 打赏
  • 举报
回复
谢谢各位了,终于比较彻底的弄懂了rand().谢谢
qinjuning 2011-04-12
  • 打赏
  • 举报
回复
求在范围[MinValue,MaxValue]的随机数的一个推荐方式如下:
randomValue=((double)rand()/RNAD_MAX)*(MaxValue-MinValue+1)+MinValue ;

rand()函数返回的值为[(随机种子),Rand_MAX)的一个伪随机数,默认情况下种子为1。因为rand()函数在调用前总会调用srand()函数,获得新的随机种子。因此,为了确保种子的不同,应在rand()函数调用前,先调用srand((unsinged)time(NULL)).
Ulfsaar 2011-04-12
  • 打赏
  • 举报
回复
int n = rand()%10 +1;

64,637

社区成员

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

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