Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@fabiogaluppo
Created October 23, 2016 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabiogaluppo/0fb6952e6a3bb1ca533762bbf897eb33 to your computer and use it in GitHub Desktop.
Save fabiogaluppo/0fb6952e6a3bb1ca533762bbf897eb33 to your computer and use it in GitHub Desktop.
#include <random>
namespace internals
{
using random_engine = std::default_random_engine;
static thread_local random_engine current_engine;
inline random_engine::result_type rd()
{
return std::random_device{}();
}
inline random_engine& rand_engine()
{
static thread_local random_engine engine{ rd() };
return engine;
}
inline void seed_rand()
{
rand_engine().seed(rd());
}
inline void seed_rand(random_engine::result_type seed)
{
rand_engine().seed(seed);
}
struct rand_int_func
{
using int_distribution = std::uniform_int_distribution<int>;
int_distribution distribution;
int operator()()
{
return distribution(rand_engine());
}
int operator()(int min_inclusive, int max_inclusive)
{
return distribution(rand_engine(), int_distribution::param_type{ min_inclusive, max_inclusive });
}
};
static thread_local rand_int_func rand_int;
}
inline void seed_rand()
{
internals::seed_rand();
}
inline void seed_rand(internals::random_engine::result_type seed)
{
internals::seed_rand(seed);
}
inline int rand_int()
{
return internals::rand_int();
}
inline int rand_int(int min_inclusive, int max_inclusive)
{
return internals::rand_int(min_inclusive, max_inclusive);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment