How to use operator()(Generator& g, const param_type& params);
of std::uniform_int_distribution
. This is my code
#include <iostream>
#include <random>
int get_random_int(const int lower_limit, const int upper_limit) {
std::random_device rd{};
std::mt19937 gen{rd()};
static std::uniform_int_distribution<> dist;
// use operator()(Generator& g, const param_type& params);
return dist(gen, lower_limit, upper_limit);
}
int main() {
std::cout << get_random_int(1, 1000000) << std::endl;
}
3
You need to create an object of type param_type
.
return dist(gen, std::uniform_int_distribution<>::param_type{lower_limit, upper_limit});
You can shorten the name to decltype(dist)::param_type
, though the syntex isn’t all that more readable. And of course it could benefit from some name aliases as well.
Implicit construction with just {lower_limit, upper_limit}
doesn’t work: https://godbolt.org/z/4cr3f758K, although I don’t see a requirement in cppreference that would mandate explicit
constructor.
0