#include<time.h>
//init random number "guess"
srand((unsigned)time(NULL));
int RANGE_MIN = 1000;
int RANGE_MAX = 9999;
guess = (((double) rand() /
(double) RAND_MAX) * RANGE_MAX + RANGE_MIN);
我恰好前两天写了一个小程序用到了随机数,注意头文件要有time.h;先用srand()生成随机种子,然后用rand()求随机数。指定范围可以参看程序。