random: remove generator choice from easy API
it's supposed to be an easy replacement for std::rand, we don't need to let the user choose their generator.
This commit is contained in:
parent
31afd9dcfd
commit
107947899c
@ -48,7 +48,7 @@ tmpname (std::string &str, size_t length)
|
||||
|
||||
str.resize (length);
|
||||
std::generate_n (str.begin (), length, [&] (void) {
|
||||
return util::rand::choose (alphanum);
|
||||
return util::random::choose (alphanum);
|
||||
});
|
||||
}
|
||||
|
||||
|
56
random.hpp
56
random.hpp
@ -14,36 +14,52 @@
|
||||
* Copyright 2016 Danny Robson <danny@nerdcruft.net>
|
||||
*/
|
||||
|
||||
#ifndef __UTIL_RANDOM_HPP
|
||||
#define __UTIL_RANDOM_HPP
|
||||
#ifndef CRUFT_UTIL_RANDOM_HPP
|
||||
#define CRUFT_UTIL_RANDOM_HPP
|
||||
|
||||
#include <random>
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
namespace util::rand {
|
||||
//-------------------------------------------------------------------------
|
||||
using default_generator = std::minstd_rand;
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <typename Generator = default_generator>
|
||||
Generator&
|
||||
thread_engine (void)
|
||||
namespace util::random {
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
/// return correctly initialised thread-local generator of an unspecified,
|
||||
/// but not entirely useless, type. ie, not LCG.
|
||||
auto&
|
||||
generator (void)
|
||||
{
|
||||
std::random_device rd;
|
||||
thread_local Generator gen (rd ());
|
||||
static thread_local std::mt19937_64 gen { std::random_device {}() };
|
||||
return gen;
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
template <typename Generator = default_generator, typename T, size_t N>
|
||||
T&
|
||||
choose (T (&t)[N], Generator gen = thread_engine<Generator> ())
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
/// select a value uniformly from the range [lo, hi)
|
||||
template <typename T>
|
||||
std::enable_if_t<std::is_floating_point_v<T>,T>
|
||||
uniform (T lo, T hi)
|
||||
{
|
||||
std::uniform_int_distribution<size_t> dist (0, N-1);
|
||||
return t[dist (gen)];
|
||||
return std::uniform_real_distribution { lo, hi } (generator ());
|
||||
}
|
||||
|
||||
|
||||
//-------------------------------------------------------------------------
|
||||
/// select a value uniformly from the range [lo, hi)
|
||||
template <typename T>
|
||||
std::enable_if_t<std::is_integral_v<T>,T>
|
||||
uniform (T lo, T hi)
|
||||
{
|
||||
return std::uniform_int_distribution { lo, hi } (generator ());
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
/// choose a value at random from an array
|
||||
template <typename T, size_t N>
|
||||
T&
|
||||
choose (T (&t)[N])
|
||||
{
|
||||
std::uniform_int_distribution<size_t> dist (0, N - 1);
|
||||
return t[dist (generator ())];
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user