rand()函数不能正常工作? [英] rand() function doesn't work well??
问题描述
亲爱的,
我尝试使用rand()生成一些整数;
但这些整数从不大于40000。
我使用Borland C ++ Builder进行测试,结果也一样。
但我在CYGWIN中使用g ++,结果很完美。
有什么问题?
这是我的测试代码。
----------------- --------------------------
#include< cstdlib>
#include < iostream>
#include< ctime>
使用命名空间std;
int main()
{
srand(time(NULL));
int count = 0;
for(;;){
int value = rand();
if(value> 40000){
cout<<值<< endl;
if(count ++> 10){
break;
}
}
}
返回0;
}
----------------- ------------------------
Dear all,
I try to use rand() to generate some integers;
but these integers are never larger than 40000.
I use Borland C++ Builder to test, the result is also same.
But I use g++ in CYGWIN, the result is perfect.
What''s wrong?
Here is my test code.
-------------------------------------------
#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
srand(time(NULL));
int count=0;
for(;;) {
int value=rand();
if (value>40000) {
cout << value << endl;
if (count++ >10) {
break;
}
}
}
return 0;
}
-----------------------------------------
推荐答案
cylin < CY *** @ avant.com.tw>在消息中写道
新闻:2t ************* @ uni-berlin.de ...
"cylin" <cy***@avant.com.tw> wrote in message
news:2t*************@uni-berlin.de...
亲爱的,>
我尝试使用rand()生成一些整数;
但这些整数从不大于40000.
我使用Borland C ++ Builder进行测试,结果也是同样。
但我在CYGWIN中使用g ++,结果很完美。
有什么问题?
Dear all,
I try to use rand() to generate some integers;
but these integers are never larger than 40000.
I use Borland C++ Builder to test, the result is also same.
But I use g++ in CYGWIN, the result is perfect.
What''s wrong?
Knuth,电脑艺术编程,第三卷和数学分析
的来源(如果提供)可能会告诉你。
Knuth, the art of computer programming, Vol III and a mathematical analysis
of the sources (if provided) will probably tell you.
cylin写道:
亲爱的,
我尝试使用rand()生成一些整数;
但这些整数从不大于40000.
但我在CYGWIN中使用g ++,结果很完美。
有什么问题?
Dear all,
I try to use rand() to generate some integers;
but these integers are never larger than 40000.
I use Borland C++ Builder to test, the result is also same.
But I use g++ in CYGWIN, the result is perfect.
What''s wrong?
RTFM
" rand函数返回一个伪ndom整数,范围为0到
RAND_MAX"
-
Sigurd
http://utvikling.com
" cylin" < CY *** @ avant.com.tw>在留言新闻中写道:2t ************* @ uni-berlin.de ...
"cylin" <cy***@avant.com.tw> wrote in message news:2t*************@uni-berlin.de...
亲爱的,
我试着使用rand()生成一些整数;
但这些整数从不大于40000.
我使用Borland C ++ Builder进行测试,结果也一样。
但是我在CYGWIN中使用g ++,结果很完美。
有什么问题?
你对RAND_MAX
大于32767的期望是什么错误。你误认为
发生的结果为了满足您对
的错误预期,确认您的期望是好的。事实
是RAND_MAX no
大于32767的实现仍然可以(完美地)符合G / b
对C的标准要求库。
如果你真的需要更大范围的随机整数,
要么选择一个编译器和运行时库,那就是
你想要什么,或找到一个伪随机数
发电机。很多都漂浮在周围。
这是我的测试代码。
Dear all,
I try to use rand() to generate some integers;
but these integers are never larger than 40000.
I use Borland C++ Builder to test, the result is also same.
But I use g++ in CYGWIN, the result is perfect.
What''s wrong?
What''s wrong is your expectation that RAND_MAX
will be greater than 32767. You mistake a result that
happened to meet your incorrect expectation for
confirmation that your expectation is good. The fact
is that an implementation with RAND_MAX no
greater than 32767 can still conform (perfectly) to
the standard requirements placed on the C library.
If you truly need a larger range of random integers,
either pick a compiler and runtime libary that does
what you want, or find a pseudo-random number
generator that does. Many are floating around.
Here is my test code.
[因为没有问题而削减]
-
- 拉里布拉斯菲尔德
电子邮件:做*********************** @ hotmail.com
以上观点可能仅属于我。
[cut because not at issue]
--
--Larry Brasfield
email: do***********************@hotmail.com
Above views may belong only to me.
这篇关于rand()函数不能正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!