refactor UF_random_generator to fit the new std::shuffle

This commit is contained in:
Johann Dreo 2022-01-23 18:17:48 +01:00
commit 4ee48e760b
7 changed files with 39 additions and 29 deletions

View file

@ -193,17 +193,19 @@ class eoInitPermutation: public eoInit<EOT> // FIXME inherit from eoInitWithDim
virtual void operator()(EOT& chrom)
{
chrom.resize(chromSize);
for(unsigned idx=0;idx <chrom.size();idx++)
chrom[idx]=idx+startFrom;
for(unsigned idx=0; idx < chrom.size(); idx++) {
chrom[idx] = idx+startFrom;
}
std::shuffle(chrom.begin(), chrom.end(),gen);
UF_random_generator<unsigned int> gen(chrom.size());
std::shuffle(chrom.begin(), chrom.end(), gen);
chrom.invalidate();
}
private :
unsigned chromSize;
unsigned startFrom;
UF_random_generator<unsigned int> gen;
// UF_random_generator<unsigned int> gen;
};
/** @example t-eoInitPermutation.cpp
*/

View file

@ -89,7 +89,8 @@ class boolean_generator
either between [0, _max) if only one value (_max) is given to the ctor
or in [_min,_max) if 2 values are given (_min, _max)
*/
template <class T = uint32_t> class random_generator
template <class T = uint32_t>
class random_generator
{
public :
// added new ctor with 2 params, and modified the data to minim and range
@ -123,16 +124,25 @@ inline bool random_generator<bool>::operator()(void)
function (see eoPop::shuffle): its operator() takes an unsigned argument m
and must return an unsigned uniformly distributed in [0,m}
*/
template <class T = uint32_t> class UF_random_generator
template <class T = uint32_t>
class UF_random_generator
{
public :
UF_random_generator(eoRng& _rng = rng) :
random(_rng) {}
public :
using result_type = T;
T operator()(T _t) { return (T) (random.random(_t)); }
UF_random_generator(T max, eoRng& _rng = rng)
: _max(max), _random(_rng)
{}
private :
eoRng& random;
T operator()() { return _random.random(_max); }
T operator()(T m) { return _random.random(m); }
T min() { return 0; }
T max() { return _max; }
private :
T _max;
eoRng& _random;
};