c++ Pseudo-random generator wrapper I wrote a simple wrapper for the &lt;random&gt; header, as follows:

#include &lt;random&gt;

namespace Random
{

    static int getInt(int min, int max)
    {
        std::random_device rd;
        std::mt19937 gen(rd());
        std::uniform_int_distribution&lt;&gt; distribution(min, max);

        int rnd = distribution(gen);

        return rnd;
    }

    static float getFloat(float min, float max)
    {
        std::random_device rd;
        std::mt19937 gen(rd());
        std::uniform_real_distribution&lt;&gt; distribution(min, max);

        float rnd = distribution(gen);

        return rnd;
    }

}


I basically just want to know: is there a better / more efficient way to do it?

I know for a fact that it works quite well, but I am no expert. I thank you in advance for your answers.
You might be able to save the overhead of constructing a new generator on each call, with something like:

static std::mt19937 default_generator(std::random_device{}());

static int getInt(int min, int max)
{
    return std::uniform_int_distribution{min, max}(default_generator);
}


But, given that most uses require several values from a single distribution and there's rarely a need to use more than one generator, is this header really worth its cognitive load?  Each function is a one-liner, and even less if the distribution can be re-used over several calls.

We'd also expect to have versions for longer or unsigned integer types, and for double and other floating-point types.  It probably makes more sense for it to be a template:

#include &lt;random&gt;
#include &lt;type_traits&gt;

template&lt;typename T&gt;
inline T getUniformRandom(T min, T max)
{
    static std::mt19937 default_generator(std::random_device{}());
    if constexpr (std::is_integral_v&lt;T&gt;) {
        return std::uniform_int_distribution{min, max}(default_generator);
    } else if constexpr (std::is_floating_point_v&lt;T&gt;) {
        return std::uniform_real_distribution{min, max}(default_generator);
    }

    static_assert(0, "getUniformRandom requires an arithmetic type");
    return min+max;             // just to eliminate compiler warnings
}


Alternatively, use std::enable_if instead of if constexpr:

#include &lt;random&gt;
#include &lt;type_traits&gt;

template&lt;typename T&gt;
inline std::enable_if_t&lt;std::is_integral_v&lt;T&gt;, T&gt; getUniformRandom(T min, T max)
{
    static std::mt19937 default_generator(std::random_device{}());
    return std::uniform_int_distribution{min, max}(default_generator);
}

template&lt;typename T&gt;
inline std::enable_if_t&lt;std::is_floating_point_v&lt;T&gt;, T&gt; getUniformRandom(T min, T max)
{
    static std::mt19937 default_generator(std::random_device{}());
    return std::uniform_real_distribution{min, max}(default_generator);
}