【发布时间】:2012-12-30 11:43:44
【问题描述】:
我正在尝试这样做,以便在执行代码时,我可以在 命令提示符重新定义最大数并在 0 和新的最大数之间生成一个新的随机数。
我做对了吗?
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main()
{
int randomnumber ;
int max;
srand( time(NULL) );
scanf("Enter total number of students %d",&max);
randomnumber=rand()% 30;
printf("This is your random number\n %d",randomnumber);
getchar();
return 0;
}
【问题讨论】:
-
这样做:
randomnumber = rand()% max; -
Modulo 对此并不十分幸运。检查stackoverflow.com/a/9515646/1168156