C/C++编程产生指定范围内的随机数,直接上个小程序:
#include <stdlib.h> // 对应于C++中cstdlib#include <time.h> // ctime#include <stdio.h>int main(){srand(time(NULL));int low = 0, high = 100;int rnum = rand() % (high - low + 1) + low;printf("random number = %d\n", rnum);system("pause");return 0;}
调用rand()会产生[0,32757]之间的随机数,(high - low)的绝对值不能超过32767。