正确使用 s/rand 或 Boost::random

发布于 2024-08-11 06:21:46 字数 1212 浏览 3 评论 0原文

我知道这种问题已经被问过几次了,但其中很多答案都归结为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

◇流星雨 2024-08-18 06:21:46

使用选项 2,您不应为每次调用创建 rngdie 的新实例。

我也不确定我是否正确理解选项 1 的问题:您只调用 srand() 一次,但每次调用 rand() 都会给您相同的数字?那是..“不寻常”。尝试将循环减少到最少,并打印 rand() 返回的值。

With option 2, you shouldn't create a new instance of rng and die 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().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文