转载于:https://blog.csdn.net/garfielder007/article/details/51386490
C/C++编程产生指定范围内的随机数,直接上个小程序:
-
#include <stdlib.h> // 对应于C++中cstdlib
-
#include <time.h> // ctime
-
#include <stdio.h>
-
-
int main()
-
{
-
s
rand(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。