如何解决带有随机两种状态基准的错误结果
我将此代码用于初步基准测试,该标准比较了使用缩放双随机值和使用伯努利分布生成一定数量的随机状态所花费的时间。 >。代码如下:
df[which.max(df$goals),1]
对于int main()
{
std::random_device s;
std::mt19937 engine(s());
std::bernoulli_distribution bernp50(0.5000000000000000);
std::uniform_real_distribution<double> d;
long int limit = 10000000000; //10^10
int counter[2] = {0};
{
Timer bernstate("Bern Two States");
for(int i = limit; i>0; i--)
{
int tmp = bernp50(engine);
//Implicit bool to int conversion
counter[tmp]++;
}
}
cout << " Bern Two States - 0,1 \n\nCounter:\n" << "0: " <<
counter[0] <<"\n1: " << counter[1]<<"\n"
<< "Counter additions: " << counter[0] + counter[1] << "\n\n"
<< "\n0: " << (double)((double)counter[0]*100/(double)limit) << "%"
<< "\n1: " << (double)((double)counter[1]*100/(double)limit) << "%"
<< "\n\n" << endl;
counter[0]=0;
counter[1]=0;
{
Timer double_comp("Two State - Double");
for(int i = limit; i>0; i--)
{
double temp = d(engine)*2;
if(temp < 1)
{
counter[0]++;
}
else
{
counter[1]++;
}
}
}
cout << " Double Two States - 0,1 \n\nCounter:\n" << "0: " <<
counter[0] <<"\n1: " << counter[1]<<"\n"
<< "Counter additions: " << counter[0] + counter[1] << "\n\n"
<< "\n0: " << (double)((double)counter[0]*100/(double)limit) << "%"
<< "\n1: " << (double)((double)counter[1]*100/(double)limit) << "%"
<< "\n\n" << endl;
} //End of Main()
,我得到的结果是计数器的加法数大于限制变量。与limit = 10^10
相同:
10^11
但是,对于Timer Object: Bern Two States Timer Object Destroyed: Bern Two States Duration Elapsed: 85.9409 s
Bern Two States - 0,1
Counter: 0: 705044031 1: 705021377 Counter additions: 1410065408
0: 7.05044% 1: 7.05021%
Timer Object: Two State - Double Timer Object Destroyed: Two State - Double Duration Elapsed: 87.6082 s
Double Two States - 0,1
Counter: 0: 705029886 1: 705035522 Counter additions: 1410065408
0: 7.0503% 1: 7.05036%
,结果很好:
limit = 10^9
解决方法
已解决:我实际上也将 long int 用于计数器,但问题出在循环范围迭代器上,它是一个 4 字节整数。循环实际上搞砸了。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。