I am a complete beginner with the range-v3 library.. Suppose I want to fill a std::array
with random numbers in some interval.
With iterators, I'd do something like this answer, passing iterators to my std::array
in question as arguments.
template< class Iter >
void fill_with_random_int_values( Iter start, Iter end, int min, int max)
{
static std::random_device rd; // you only need to initialize it once
static std::mt19937 mte(rd()); // this is a relative big object to create
std::uniform_int_distribution<int> dist(min, max);
std::generate(start, end, [&] () { return dist(mte); });
}
With the ranges library, I wanted to use ranges::view::generate_n
, with a unary function that generates a single random number along with the size of my array.
auto random_num() -> int {
static std::mt19937 engine{std::random_device{}()};
static std::uniform_int_distribution<int> dist(1, 10);
return dist(engine);
}
std::vector<int> nums = ranges::view::generate_n(random_num, 10);
This works well for a std::vector
, but I am rather lost on what algorithm I should be using to fill a std::array
rather than generate a std::vector
, as a similar approach to above does not work. I can transform
the array and disregard each argument but that doesn't seem right.