C++ OOP: how to create a random number generator object?
<p>So this question is sort of one of translation.  I am new to C++, and was looking through the class documentation.  However, it looks like finding the answer to my question is a bit hard via the documentation. </p>

<p>I have code for generating a random number between 0 and 1 in C++: (obtained from <a href="http://en.cppreference.com/w/cpp/numeric/random/uniform_real_distribution" rel="nofollow">here</a>, since the rand() function solution for floats is integer based)</p>

<pre><code>#include &lt;random&gt;
#include &lt;iostream&gt;

int main()
{
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_real_distribution&lt;&gt; dis(0, 1); //corrected from 1,2
    for (int n = 0; n &lt; 10; ++n) {
        std::cout &lt;&lt; dis(gen) &lt;&lt; ' ';
    }
    std::cout &lt;&lt; '\n';
}
</code></pre>

<p>Next, I would like to create a class or struct or something (not really an OOP guy) that has an API like: </p>

<pre><code>float x = my_RandomNumberGenerator.next();
</code></pre>

<p>In python, I might write something like: </p>

<pre><code>class my_RNG():
    def __init__(self):
        self.rd = (the random device object I initialize in c code)
        self.gen = (the mersenne_twister engine object)(rd)
        self.distribution = (the uniform real distribution object)

    def next():
        return self.distribution(self.gen)

my_randomNumberGenerator = my_RNG()
print(my_randomNumberGenerator.next())
</code></pre>

<p>How would I implement this in C++? </p>

<p><strong>update</strong> Here is what I have so far (it does not work... or compile...but there seems to be some strangeness in the way things are initialized in my template code that I got from the reference site that I don't understand): </p>

<pre><code>#include &lt;iostream&gt;
#include &lt;random&gt;


class MyRNG
{
    public:
        float next(void);
    private:
        std::random_device randomDevice;
        std::mt19937_64 randomGenerator;
        std::uniform_real_distribution distribution;
        MyRNG(float range_lower,float range_upper);

};
MyRNG::MyRNG(float range_lower, float range_upper)
{
    randomGenerator = std::mersenne_twister_engine(randomDevice);
    distribution = std::uniform_real_distribution&lt;&gt; distribution(range_lower,range_upper);
}
MyRNG::next(void)
{
    return distribution(randomGenerator);
}

int main() {
    MyRNG my_rng = MyRNG(0,1);
    std::cout &lt;&lt; my_rng.next() &lt;&lt; std::endl;
    return 0;
}
</code></pre>

<p>Seems like you just need some form of probability generation class, see below for a basic implementation which meets your question requirements:</p>

<pre><code>template&lt;class Ty = double,
    class = std::enable_if_t&lt;std::is_floating_point&lt;Ty&gt;::value&gt;
&gt; class random_probability_generator {
public:
    // default constructor uses single random_device for seeding
    random_probability_generator() 
        : mt_eng{std::random_device{}()}, prob_dist(0.0, 1.0) {}
    // ... other constructors with custom seeds if necessary
    Ty next() { return prob_dist(mt_eng); } 
    // ... other methods if necessary
private:
    std::mt19937 mt_eng;
    std::uniform_real_distribution&lt;Ty&gt; prob_dist;
};
</code></pre>

<p>Then you can use this simply via:</p>

<pre><code>random_probability_generator&lt;&gt; pgen;
double p = pgen.next(); // double in range [0.0, 1.0]
</code></pre>

<p>Or if you want random <code>float</code>s instead (as part of your question seems to imply):</p>

<pre><code>random_probability_generator&lt;float&gt; pgen;
float p = pgen.next(); // float in range [0.0f, 1.0f] 
</code></pre>

<hr>

<p>Also, to address why the class you posted isn't compiling, the error in your class is that you try to initialise a <code>std::mt19937_64</code> type object (<code>randomGenerator</code>) with a <code>std::mersenne_twister_engine</code> instance but they are fundamentally different types. Instead you would need to do </p>

<pre><code>randomGenerator = std::mt19937_64(randomDevice());
</code></pre>

<p>in <code>MyRNG</code> constructor, or construct via initialisation list as I have done in the example above. </p>

<hr>

<p>As pointed out in the comments, a more suitable <em>c++-esque</em> implementation of this is to overload <code>operator()</code> instead of creating a <code>next()</code> method. See below for a better implementation of the above class,</p>

<pre><code>template&lt;class FloatType = double,
    class Generator = std::mt19937,
    class = std::enable_if_t&lt;std::is_floating_point&lt;FloatType&gt;::value&gt;
&gt; class uniform_random_probability_generator {
public:
    typedef FloatType result_type;
    typedef Generator generator_type;
    typedef std::uniform_real_distribution&lt;FloatType&gt; distribution_type;
    // default constructor
    explicit uniform_random_probability_generator(Generator&amp;&amp; _eng 
        = Generator{std::random_device{}()}) : eng(std::move(_eng)), dist() {}
    // construct from existing pre-defined engine
    explicit uniform_random_probability_generator(const Generator&amp; _eng)
        : eng(_eng), dist() {}
    // generate next random value in distribution (equivalent to next() in above code)
    result_type operator()() { return dist(eng); }
    // will always yield 0.0 for this class type
    constexpr result_type min() const { return dist.min(); }
    // will always yield 1.0 for this class type
    constexpr result_type max() const { return dist.max(); }
    // resets internal state such that next call to operator() 
    // does not rely on previous call
    void reset_distribution_state() { dist.reset(); }
private:
    generator_type eng;
    distribution_type dist;
};
</code></pre>

<p>Then you can use this similarly to the first class in this answer,</p>

<pre><code>uniform_random_probability_generator&lt;&gt; urpg;
double next_prob = urpg();
</code></pre>

<p>Additionally, <code>uniform_random_probability_generator</code> can use a different <code>Generator</code> type as a template parameter so long as this type meets the requirements of <a href="http://en.cppreference.com/w/cpp/concept/UniformRandomBitGenerator" rel="nofollow"><code>UniformRandomBitGenerator</code></a>. For example, if for any reason you needed to use <code>std::knuth_b</code> instead of <code>std::mt19937</code> then you can do so as follows:</p>

<pre><code>uniform_random_probability_generator&lt;double, std::knuth_b&gt; urpg_kb;
double next_prob = urpg_kb();
</code></pre>

python c++ oop