正确使用 s/rand 或 Boost::random
我知道这种问题已经被问过几次了,但其中很多答案都归结为 RTFM,但我希望如果我能提出正确的问题......我可以为其他人得到准明确的答案嗯,关于实施。
我尝试通过以下两种方式之一生成随机数序列:
#include <cstdlib>
#include <ctime>
#include <cmath>
#include "Cluster.h"
#include "LatLng.h"
srand((unsigned)time(0));
double heightRand;
double widthRand;
for (int p = 0; p < this->totalNumCluster; p++) {
Option 1.
heightRand = myRand();
widthRand = myRand();
Option 2.
heightRand = ((rand()%100)/100.0);
widthRand = ((rand()%100)/100.0);
LatLng startingPoint( 0, heightRand, widthRand );
Cluster tempCluster(&startingPoint);
clusterStore.insert( clusterStore.begin() + p, tempCluster);
}
其中 myRand() 是:
#include <boost/random.hpp>
double myRand()
{
boost::mt19937 rng;
boost::uniform_int<> six(1,100);
boost::variate_generator<boost::mt19937&, boost::uniform_int<> > die(rng, six);
int tempDie = die();
double temp = tempDie/100.0;
return temp;
}
每次运行选项 1 时,每次执行每个循环时都会得到相同的数字。但每次运行程序时都会有所不同。
当我运行选项 2 时,我从 boost 库中得到 82,因此返回 0.81999999999999。如果是 42 我可以理解,但即使在阅读了 boost random 文档之后,82 还是让我摸不着头脑。
有什么想法吗?
DJ。
I know this kind of question has been asked a few times, but alot of them answers boil down to RTFM, but I'm hoping if I can ask the right question... I can get a quasi-definitive answer for everyone else as well, regarding implementation.
I'm trying to generate a sequence of random numbers in one of the two following ways:
#include <cstdlib>
#include <ctime>
#include <cmath>
#include "Cluster.h"
#include "LatLng.h"
srand((unsigned)time(0));
double heightRand;
double widthRand;
for (int p = 0; p < this->totalNumCluster; p++) {
Option 1.
heightRand = myRand();
widthRand = myRand();
Option 2.
heightRand = ((rand()%100)/100.0);
widthRand = ((rand()%100)/100.0);
LatLng startingPoint( 0, heightRand, widthRand );
Cluster tempCluster(&startingPoint);
clusterStore.insert( clusterStore.begin() + p, tempCluster);
}
Where myRand() is:
#include <boost/random.hpp>
double myRand()
{
boost::mt19937 rng;
boost::uniform_int<> six(1,100);
boost::variate_generator<boost::mt19937&, boost::uniform_int<> > die(rng, six);
int tempDie = die();
double temp = tempDie/100.0;
return temp;
}
Every time I run Option 1, I get the same number on each execution of each loop. But different on each run of the program.
When I run Option 2, I get 82 from the boost libraries, so 0.81999999999999 is returned. I could understand if it was 42, but 82 is leaving me scratching my head even after reading the boost random docs.
Any ideas?
DJS.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用选项 2,您不应为每次调用创建
rng
和die
的新实例。我也不确定我是否正确理解选项 1 的问题:您只调用 srand() 一次,但每次调用 rand() 都会给您相同的数字?那是..“不寻常”。尝试将循环减少到最少,并打印 rand() 返回的值。
With option 2, you shouldn't create a new instance of
rng
anddie
for each call.I'm also not sure if I understand the problem with option 1 correctly: you are calling srand() only once, but each call to rand() gives you the same number? That's.. "unusual". Try to reduce the loop to the minimum, and print the value returned from rand().