Proper use of s / rand or Boost :: random

I know this question has been asked several times, but many of them answer the RTFM question, but I hope that if I can ask the right question ... I can get a quasi-final answer for everyone else regarding the implementation.

I am trying to create a sequence of random numbers in one of the following two 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 is 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;
}

Each time I run Option 1, I get the same number every time I run each cycle. But different for each program launch.

When I run Option 2, I get 82 from boost libraries, so 0.81999999999999 is returned. I could understand if it was 42, but 82 leaves me scratching my head even after reading accelerated random documents.

Any ideas?

DJS.

0
1

2 rng die .

, 1: srand() , rand() ? ... "". , rand().

+3

Source: https://habr.com/ru/post/1746875/


All Articles