Portably generate uniformly random floats from mt19937 output
At least in my opinion, the efficiency question you've raised (repeated division by 2 vs. division by 232 vs. multiplication by 2-32) is best answered by profiling.

I, however, would take issue with making a blind assumption that the generator's maximum is 232-1 at all. Instead, I'd rather use the generator's own specification of the maximum value it can return, so the code would look something like this:

return (float)rng() / (float)RNG::max();


Here again, pre-inverting and multiplying might (possibly) improve speed:

static const float factor = 1.0f / RNG::max();

return rng() * factor;


I also tend to wonder whether it might make sense to make the result type generic. With this, we're no longer restricted to a generator that produces a 32-bit result, but we probably do want to assure that the result type is floating point.

template &lt;typename Result, typename RNG&gt;
Result uniform_dist(RNG &amp; rng) {
    static_assert(std::is_floating_point&lt;Result&gt;::value, "Result must be a floating point type");

    static const Result factor = Result(1) / static_cast&lt;Result&gt;(RNG::max());

    return rng() * factor;
}


Then we'd specify the result type as a template parameter:

std::mt19937 rng { std::random_device()() };

std::cout &lt;&lt; uniform_dist&lt;float&gt;(rng) &lt;&lt; "\n";

std::cout &lt;&lt; uniform_dist&lt;double&gt;(rng) &lt;&lt; "\n";