Several month ago I had downloaded a technical paper (pdf) on random number generation in C++11 and have been digesting the contents.
The paper: http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2013/n3551.pdf
One part of the paper really intrigued me, the simple toolkit. I noticed it was written as stand-alone functions. I decided to see if I could encapsulate the functionality into a class.
Long story short, it works for me. But I don't know if there are potential problems that could show up, so would like some expert advice.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
class URNG
{
public:
URNG() {}
~URNG() {}
public:
std::default_random_engine& GetGenerator() { return dre; }
public:
void randomize();
public: // generic random number generation functions
int pick_a_number(const int from, const int thru);
double pick_a_number(const double from, const double upto);
private:
std::default_random_engine dre;
};
void URNG::randomize()
{
std::random_device rd {};
dre.seed(rd());
}
int URNG::pick_a_number(const int from, const int thru)
{
static std::uniform_int_distribution<> dist {};
using parm_t = decltype(dist)::param_type;
return dist(dre, parm_t { from, thru });
}
double URNG::pick_a_number(const double from, const double upto)
{
static std::uniform_real_distribution<> dist {};
using parm_t = decltype(dist)::param_type;
return dist(dre, parm_t { from, upto });
}
|
I do know that using default_random_engine will not work with GCC-based compilers, it is a known bug as several people pointed out in other topics. It seems to work with VS 2015 without causing problems.
This was done as a learning experience, there is a lot of real world considerations I deliberately avoided dealing with.
Was encapsulating the stand-alone functions in a class a wrong approach? Are there hidden "undefined errors" lurking that I don't see?