![](/img/trans.png)
[英]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.