#include<time.h>
#include<algorithm>
#include<functional>
#include <iostream>
#include<random>
using namespace std;
namespace stackoverflow
{
//just for this topic
template<typename _FwdIt,typename _RngFn>
void random_fill(_FwdIt first, _FwdIt last, _RngFn&& fn)
{ //random fill the range [first,last) by means of fn
_DEBUG_RANGE(first, last);
generate(first, last, fn);
}
}
//random function-fn is provided by yourself, for example
int main() {
using namespace stackoverflow;
static default_random_engine e(time(0));
std::normal_distribution<> rnorm{ 0,1 };
std::uniform_int_distribution<> runifInt{ 0,1 };
std::uniform_int_distribution<> runifInt_1{ 2,10 };
std::uniform_real_distribution<> runifDouble{ 0,1 };
vector<int> x(10);
//type int can be random fill throw normal or uniform distribution or other, and
//some distribution parameter can change
random_fill(x.begin(), x.end(), bind(ref(runifInt),ref(e)));
random_fill(x.begin(), x.end(), bind(ref(runifInt_1), ref(e)));
random_fill(x.begin(), x.end(), bind(ref(rnorm), ref(e)));
vector<double> y(10);
//type double can be random fill throw normal or uniform distribution or other, and
//some distribution parameter can change
random_fill(y.begin(), y.end(), bind(ref(rnorm),ref(e)));
random_fill(y.begin(), y.end(), bind(ref(runifDouble), ref(e)));
return 0;
}
Я думаю, что алгоритм STL является наиболее полезным, поэтому я создаю эти мутирующие STL.Для объекта, определенного в главной функции, вы можете переместить их в пространство имен stackoverflow, но не делать их глобальными.