林這樣的功能:生成簡單的隨機數與時間在C
int rand2(int lim)
{
static long a = 1; // could be made the seed value
a = (a * 32719 + 3) % 32749;
return ((a % lim) + 1);
}
獲得隨機數的堆棧,它做工精細,但每次我啓動此功能時,我將有許多相同的堆棧,所以我想使用的時間()系統函數每次有不同的堆棧
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);
}
後來我給我的電腦的一個時間的時間(),因爲變量是靜態
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不是恆定的,但因爲我用它一個時間,我不知道爲什麼
一個類似的問題[爲什麼編譯器限制全局變量總是被常量值初始化?](http://stackoverflow.com/questions/19130321/why-compiler-restricts-global-variable-always-to-be-初始化的常量值) – 2013-10-02 09:01:48
我知道沒有允許此代碼的C編譯器。你甚至試圖編譯它?如果是這樣,你需要一個新的編譯器。 – Lundin