[英]Is there a nanosleep function for “struct timeval” in MacOS?
[英]declare struct timeval time inside a function
我已經使用time.h獲取隨機種子初始化時對問題進行了一些搜索。 特別是在我的情況下,我想將時間放置在主要功能之外。
根據評論,我進行了一些更改。 在include as和中包含兩次后,這些錯誤和警告消失了:
too few arguments to function ‘random’
implicit declaration of function ‘srandom’ [-Wimplicit-function-declaration]
too few arguments to function ‘random’
一旦我將代碼聲明為:
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>
#include <unistd.h>
#include <math.h>
int main(void)
{
struct timeval time;
...
}
它可以工作,但是必須移到main之外。 但是,如果我更改為:
#include <sys/time.h>
struct timeval time;
int main(void)
{
...
}
它給了我錯誤:
‘time’ redeclared as different kind of symbol
就我而言,我正在使用的實現不是在C ++中,而是在C中 。 當我嘗試將代碼移至某個函數以便每次調用我的函數時收到一個不同的隨機數時,會發生一些其他錯誤:
double myRandom(double dAverage,double dStddev);
double myRandom(double dAverage,double dStddev){
int iRandom1, iRandom2;
double dRandom1, dRandom2,result;
long int liSampleSize;
iRandom1=(random());
dRandom1=(double)iRandom1 /2147483647;
iRandom2=(random());
dRandom2=(double)iRandom2 /2147483647;
result= dAverage + dStddev * sqrt(-2.0 * log(dRandom1))*cos(6.28318531 * dRandom2);
return(result);
}
int main(void){
...
struct timeval time;
gettimeofday(&time, NULL);
srandom((unsigned int) time.tv_usec);
for (i=0; i<liSampleSize;i++)
{ result=myRandom(dAverage,dStddev);
}
}
顯然它應該起作用 。 有誰知道這是怎么回事。 高度贊賞所有評論。
更新:然后,從myRandom中取出隨機值使生成的值符合預期。 因此,它起作用了!
第一個錯誤與time.h
有關。 包括它會為全局名稱空間引入一些稱為time
實例。
第二種可能是因為您尚未包含stdlib.h
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.