Erich Keane
Software Engineer, iCDG, Intel
Erich.Keane@verizon.net
std::random_device rd; // Instantiates a RNG!
unsigned int rand_num = rd(); // generates a random number!std::random_device rd; // Instantiates a RNG!
std::array <unsigned int, 624> entropy; // 624 is magic number for MT :(
std::generate(std::begin(entropy), std::end(entropy), std::ref(rd));
std::seed_seq seq{entropy}; // Seed Sequence Ready
std::mt19937 engine{seq}; // Create engine for MT random numbers
unsigned int rnd_val = engine(); // rnd_val is a random number!std::random_device rd; // Instantiates a RNG!
std::array <unsigned int, 624> entropy; // 624 is magic number for MT :(
std::generate(std::begin(entropy), std::end(entropy), std::ref(rd));
std::seed_seq seq{entropy}; // Seed Sequence Ready
std::mt19937 engine{seq}; // Create engine for MT random numbers
std::uniform_int_distribution<int> int_dist{1,5};
int rand_int = int_dist(engine); // rand_int is [1,5]
std::uniform_real_distribution<double> double_dist{1, 1.5};
double rand_double = double_dist(engine); // [1, 1.5)Fixing the Seed Problem:
// Note: Not actually SeedSequence,
// lacks default/init-list ctors and params method
template <class RandType>
class GenSeq
{
public:
using result_type = typename RandType::result_type;
GenSeq(RandType& rt):_rt(rt){}
size_t size() const{ return std::numeric_limits<size_t>::max();}
template< class RandomIt>
void generate( RandomIt begin, RandomIt end)
{
std::generate(begin, end, std::ref(_rt));
}
private:
RandType& _rt;
};
int main()
{
std::random_device rd;
GenSeq<std::random_device> seq { rd };
std::mt19937 engine { seq };
std::cout << engine() << '\n';
}Erich Keane
Software Engineer, iCDG, Intel
Erich.Keane@verizon.net
template<template<typename, typename> class T, typename IT, typename Alloc>
void DoThingToVector(T<IT, Alloc> vect)
{
std::cout << typeid(IT).name() <<std::endl;
}
int main()
{
std::vector<int> iv;
std::vector<double> dv;
std::vector<std::string> sv;
DoThingToVector(iv);
DoThingToVector(dv);
DoThingToVector(sv);
}
// Results:
i
d
NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
template<template<typename, typename> class T, typename IT, typename Alloc>
void DoThingToVector(T<IT, Alloc> vect)
{
std::cout << typeid(IT).name() <<std::endl;
}
template<template<typename, typename> class T, typename Alloc>
void DoThingToVector(T<std::string, Alloc> vect)
{
std::cout << "std::string!" <<std::endl;
}
int main()
{
std::vector<int> iv;
std::vector<double> dv;
std::vector<std::string> sv;
DoThingToVector(iv);
DoThingToVector(dv);
DoThingToVector(sv);
}
// Results:
i
d
std::string!
template<template<typename...> class T, typename FST, typename SCD, typename...Rest>
void PrintSecondType(T<FST,SCD,Rest...>)
{
std::cout << typeid(SCD).name() << std::endl;
}
int main()
{
std::tuple<int, double, std::string> tp1;
std::tuple<int, float, std::string> tp2;
std::tuple<int, char, std::string> tp3;
PrintSecondType(tp1);
PrintSecondType(tp2);
PrintSecondType(tp3);
}
// Results:
d
f
c
template<typename Func>
struct func_get;
template<template <typename...> class Func, typename... Args, typename Ret>
struct func_get<Func<Ret(Args...)>>
{
template<size_t Index>
using argument_type = typename std::tuple_element<Index, std::tuple<Args...> >::type;
};
int main()
{
std::function<int(double, float, char, int)> f;
static_assert(is_same<double,func_get<decltype(f)>::argument_type<0>>::value, "Failed!");
static_assert(is_same<float,func_get<decltype(f)>::argument_type<1>>::value, "Failed!");
static_assert(is_same<char,func_get<decltype(f)>::argument_type<2>>::value, "Failed!");
static_assert(is_same<int,func_get<decltype(f)>::argument_type<3>>::value, "Failed!");
}