vc 语言中 哪个函数可以产生随即数 ?

来源:百度知道 编辑:UC知道 时间:2024/05/25 12:34:54
要用哪个头文件 ?

哪有那么麻烦
#include <time.h> //包含头文件time
void main()
{int a;
srand(time(NULL));//因为这个地方要以时间为轴作种子
a=rand()%9; //这里表示产生0~9的随机数

}
再举个例子 如果想产生3~15的随机数就这么写a=rand()%12+3;

rand产生随机数,使用前先调用srand产生种子
rand
Generates a pseudorandom number.

int rand( void );

Routine Required Header Compatibility
rand <stdlib.h> ANSI, Win 95, Win NT

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version

Return Value

rand returns a pseudorandom number, as described above. There is no error return.

Remarks

The rand function returns a pseudorandom integer in the range 0 to RAND_MAX. Use the srand function to seed the pseudorandom-num