As Richard said, you will need to keep the instance of the distribution around as long as a and b don't change. So best wrap that state in a class, after that the distribution of numbers is fine. This is the output for 100000 numbers from the example below.
value : 1, occured 24911 times
value : 2, occured 25044 times
value : 3, occured 25012 times
value : 4, occured 25033 times
#include <array>
#include <random>
#include <iostream>
// wrap your number generation in a helper class so it can maintain state.
class random_t
{
public:
// initialize generator and distribution
random_t(const int min, const int max) :
m_generator{ std::random_device{}() },
m_distribution{ min,max }
{
}
// get next random number
int operator()()
{
return m_distribution(m_generator);
}
private:
std::mt19937 m_generator;
std::uniform_int_distribution<int> m_distribution;
};
// run random generator and create a histogram
int main()
{
random_t random(1, 4);
std::array<int, 5> histogram{};
for (std::size_t n = 0; n < 100000; ++n)
{
auto value = random();
//std::cout << value << " ";
histogram[value]++;
}
for (std::size_t n = 1; n < 5; ++n )
{
std::cout << "value : " << n << ", occured " << histogram[n] << " times\n";
}
return 0;
}