我需要在 C 中编写 UNIX/LINUX srand48 和 drand48 函数。我坚持设置和使用种子值。我有两个功能:
#include <math.h>
long int mfml_win_drandX0; //actual value used for generation
void srand48(long int seedval)
{
mfml_win_drandX0=seedval; //setting seed into mfml_win_drandX0
}
double drand48(void)
{
static const double a=0x273673163155,
c=0x13,
m=281474976710656;
/*EDIT: error was here*/
mfml_win_drandX0=fmod(a*mfml_win_drandX0+c,m);; //computing the next value
return mfml_win_drandX0/m;
}
但是使用时:
srand48(2) ;
for (int i=0;i<10;i++)
std::cout<<drand48()<<std::endl;
我每次都得到相同的数字(mfml_win_drandX0)没有改变。如何解决这个问题?