VC++用哪个库函数来产生随机数?

在哪个文件啊?请说清楚!呵呵

第1个回答  2006-10-13
就是一个随机函数,有两种用法,给你讲下哈,看下面这个程序:

#include <iostream>

using std::cout;
using std::endl;

int main(){

ing n;
n = rand()%10;
cout << n << endl;

return 0;
}

在这个程序中,你运行,每次打印的都是同一个值,而这个句法也给你分析一下,rand()%10前面是固定的格式,后面这个10就是最大数,随即产生的数范围是在0-10之间,

好了,下面我们再看一段程序:

#include <iostream>

using std::cout;
using std::endl;

#include <ctime>

int main(){

int i;

srand( time(0) );

i = rand()%10;

cout << i << endl;

return 0;
}

这段程序运行了,每次结果都不一样,原因我们用了time的函数把每次都归0;所以这样能产生不同的数字。。。
第2个回答  2006-10-13
#include <stdlib.h>
#include <time.h>

然后用
srand(time(0)); //初始化
int i = rand(); //产生0--32767之间的随机数本回答被提问者采纳
相似回答