Generation of random doubles between two intervals [a, b]

I need to generate Xrandom double numbers evenly distributed between two intervals [a, b], where aand bare also double numbers.

Those numbers Xmust be generated inside the class function, say myclass::doSomething(a,b). The fact is that the intervals [a,b]were transmitted when the function changed doSomething(a,b)each time the function doSomething(a,b)is called by another function of the class, for example doThat().

I would like to get a solution that will allow me:
1. Keep enginewith a higher area, ideally, it should be visited only once for each application.
2. Random double numbers Xgenerated within each individual function call doSomething()must be evenly distributed.

My solution below does not allow to obtain a higher region for engine, and it seems that the generated numbers are not necessarily evenly distributed.

//file: utilities.h
template <typename Generator>
double randomDoubleEngine(Generator& engine, double low_bound, double high_bound )
{
        if (low_bound > high_bound){
                std::swap(low_bound, high_bound);
        }

        return std::uniform_real_distribution<>( low_bound, high_bound )( engine );
}

//file: myclass.h
       void myclass::doThat(param1, param2){

            for(int i=0; i < myclass.iter; i++){
                ...
                ...
                doSomething(a,b);
                ...
            }

        }

        void myclass::doSomething(double a, double b)
        {
                std::random_device rd;
                static std::mt19937 engine(rd());
                .....
                double curThreshold = randomDoubleEngine(engine, a, b);
                ...
         }
+4
source share
1 answer

I think you want the engine to be a static member of myclass. I'm not sure that this will make any real difference from what you have if you do not need to use the engine in other ways. I inserted a possible solution below.

, gcc (. ), , , , , ( ), , , gcc , [0,1], , .

, gcc 4.4, Ubuntu, , , std:: random_device.

#include <random>
#include <iostream>

/* In GCC 4.4, uniform_real_distribution is called uniform_real; renamed in 4.5
 *
 * However, GCC description here
 *
 * http://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-api-4.6/a00731.html
 *
 * doesn't match expectation here
 *
 * http://en.cppreference.com/w/cpp/numeric/random/uniform_real_distribution
 *
 * which seems to match 26.5.8.2.2 of N3337
 *
 */
#if defined(__GNUC_MINOR__) && (__GNUC_MINOR__ <= 4)
#  define uniform_real_distribution uniform_real
#endif

template <typename Generator>
double randomDoubleEngine(Generator& engine, double low_bound, double high_bound)
{
  if (low_bound > high_bound){
    std::swap(low_bound, high_bound);
  }
  return std::uniform_real_distribution<double>(low_bound, high_bound)(engine);
}

class myclass
{
  double curThreshold;
  static std::mt19937 engine;
  void doSomething(double a, double b)
  {
    curThreshold= randomDoubleEngine(engine, a, b);
  }
public:
  myclass(): curThreshold(0) {}

  void doThat(){
    doSomething(0,10);
    std::cout << "threshold is " << curThreshold << std::endl;
  }
};

std::mt19937 myclass::engine=std::mt19937(std::random_device()());

int
main()
{
  myclass m;
  m.doThat();
  m.doThat();
  return 0;
}
+2

All Articles