【发布时间】:2013-10-02 08:54:55
【问题描述】:
我正在使用这个功能:
int rand2(int lim)
{
static long a = 1; // could be made the seed value
a = (a * 32719 + 3) % 32749;
return ((a % lim) + 1);
}
要获得一堆随机数,它工作正常,但每次我启动这个函数时,我都会有相同的数字堆栈,所以我想使用 time() 系统函数每次都有不同的堆栈
p>int rand3(int lim, int dat_time)
{
static int a = dat_time; // could be made the seed value
a = (a * 32719 + 3) % 32749;
return ((a % lim) + 1);
}
然后我给我一次计算机的 time(),因为变量 a 是静态的
int main()
{
int rd;
time_t timee;
int seed;
timee = 0;
timee = time(timee);
seed = timee;
while(42)
{
rd = rand3(52, seed);
printf("%d\n", rd);
getchar();
}
}
然后我收到一条错误消息,说 dat_time 不是一个常数,但由于我有一次使用它,所以我不明白为什么
【问题讨论】:
-
我知道没有 C 编译器会允许此代码。你甚至尝试编译它吗?如果是这样,您需要一个新的编译器。