洛阳北大青鸟:C++和C语言编程程序随机数的产生
1:如果只要产生随机数而不需要设定范围的话,只要用rand()就行了,rand()会返回一个随机数的值,范围在RAND_MAX之间。
RAND_MAX定义在stdlib.h,它的值为2147483647.
例如:
#include
#include
void main()
{
int i ;
for(int i=0;i<10;i+)
printf("%d\n",rand());
}
(2) 如果你要随机生成一个在一定范围的数,你可以在宏定义中定义一个random(int number)函数,然后在main()里面直接调用random()函数:
例如:随机生成10个0~100的数:
#include
#include
#define random(x) (rand()%x)
void main()
{
int x;
for( x=0;x<10;x++)
printf("%d\n",random(100));
}
(3)但是上面两个例子所生成的随机数都只能是一次性的,如果你第二次运行的时候输出结果仍和第一次一样。这与srand()函数有关。srand()用来设置rand()产生随机数时的随机数种子。在调用rand()函数产生随机数前,必须先利用srand()设好随机数种子(seed), 如果未设随机数种子,rand()在调用时会自动设随机数种子为1.上面的两个例子就是因为没有设置随机数种子,每次随机数种子都自动设成相同值1 ,进而导致rand()所产生的随机数值都一样。
srand()函数定义: void srand (unsigned int seed);
通常可以利用geypid()或time(0)的返回值来当做seed
如果你用time(0)的话,要加入头文件#include
例如:
#include
#include
#include
#define random(x) (rand()%x)
void main()
{
int x;
srand((int)time(0));
for( x=0;x<10;x++)
printf("%d\n",random(100));
}