rand_max到底是多少?

2303191 2003-03-20 12:14:39
书上说rand()提供的随机数范围在 0<=rand()<=rand_max
rand_max到底是多少?
...全文
3481 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
2303191 2003-03-20
  • 打赏
  • 举报
回复
我的机器上是32767
cenlmmx 2003-03-20
  • 打赏
  • 举报
回复
系统的宏,根据机器的不同有所不同.
可以自己测试打出来看.
ww2003 2003-03-20
  • 打赏
  • 举报
回复
你可以自己看看
#include<stdlib.h>
#include<iostream.h>

void main()
{
cout<<RAND_MAX<<endl;
}
newgina 2003-03-20
  • 打赏
  • 举报
回复
根据机器的不同有所不同
zhouzhaohan 2003-03-20
  • 打赏
  • 举报
回复
而且这个和机器好像也没有什么关系,那个宏定义本身都是hard code的,并没有和int的大小相关联。
zhouzhaohan 2003-03-20
  • 打赏
  • 举报
回复
to cchzf_02()
肯定是usigned int的最大的值(32767).?
unsigned int 的最大值是32767?就算是int两个字节的话,也应该是65535吧!是不是要说signed int 啊?何况int还不一定是两个字节。
有没有必要的话,是可以乘除,但是那样其实真正能取到地值的数量还是不变的。
另外你说c++肯定是,有什么根据吗?
bm1408 2003-03-20
  • 打赏
  • 举报
回复
根据系统的不同也是不同的!
一般来说16位机器是32767
32是2^32-1
cchzf_02 2003-03-20
  • 打赏
  • 举报
回复
如果是c++的.肯定是usigned int的最大的值(32767).
根本没有必要定义成比这更大值的范围.可以通过一些乘除可以得到更大范围的值.
zhouzhaohan 2003-03-20
  • 打赏
  • 举报
回复
标准上说最小是32767,和具体的库函数有关。
7.20.2 Pseudo-random sequence generation functions
7.20.2.1 The rand function
Synopsis
1 #include <stdlib.h>
int rand(void);
Description
2 The rand function computes a sequence of pseudo-random integers in the range 0 to
RAND_MAX.
3 The implementation shall behave as if no library function calls the rand function.
Returns
4 The rand function returns a pseudo-random integer.
Environmental limits
5 The value of the RAND_MAX macro shall be at least 32767.
7.20.2.2 The srand function
Synopsis
1 #include <stdlib.h>
void srand(unsigned int seed);
Description
2 The srand function uses the argument as a seed for a new sequence of pseudo-random
numbers to be returned by subsequent calls to rand. If srand is then called with the
same seed value, the sequence of pseudo-random numbers shall be repeated. If rand is
called before any calls to srand have been made, the same sequence shall be generated
as when srand is first called with a seed value of 1.
3 The implementation shall behave as if no library function calls the srand function.
Returns
4 The srand function returns no value.
5 EXAMPLE The following functions define a portable implementation of rand and srand.
static unsigned long int next = 1;
int rand(void) // RAND_MAX assumed to be 32767
{
next = next * 1103515245 + 12345;
return (unsigned int)(next/65536) % 32768;
}
§7.20.2.2 Library 311
ISO/IEC 9899:1999 (E) ©ISO/IEC
void srand(unsigned int seed)
{
next = seed;
}


同时在windows下应该是32767,见如下连接:
http://search.microsoft.com/default.asp?qu=RAND%5FMAX&boolean=ALL&nq=NEW&so=RECCNT&p=1&ig=01&i=00&i=01&i=02&i=03&i=04&i=05&i=06&i=07&i=08&i=09&i=10&i=11&i=12&i=13&i=14&i=15&i=16&i=17&i=18&i=19&i=20&i=21&i=22&i=23&i=24&i=25&i=26&i=27&i=28&i=29&i=30&i=31&i=32&i=33&i=34&i=35&i=36&i=37&i=38&i=39&i=40&i=41&siteid=us/dev

69,371

社区成员

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

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