我是 range-v3 库的完整初学者。假设我想std::array
在某个时间间隔内用随机数填充 a 。
使用迭代器,我会做类似这个答案的事情,将迭代器std::array
作为参数传递给我的问题。
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); });
}
Run Code Online (Sandbox Code Playgroud)
对于 range 库,我想使用ranges::view::generate_n
, 和一个一元函数,该函数生成一个随机数以及数组的大小。
auto random_num() -> int {
static std::mt19937 engine{std::random_device{}()};
static std::uniform_int_distribution<int> dist(1, 10); …
Run Code Online (Sandbox Code Playgroud)