random: add unit float and ranged int generator

This commit is contained in:
Danny Robson 2018-05-30 14:33:17 +10:00
parent a90ce0920f
commit d21b7809dd

View File

@ -17,10 +17,13 @@
#ifndef CRUFT_UTIL_RANDOM_HPP
#define CRUFT_UTIL_RANDOM_HPP
#include "coord/traits.hpp"
#include <algorithm>
#include <array>
#include <random>
#include <limits>
#include <type_traits>
namespace util::random {
@ -75,17 +78,40 @@ namespace util::random {
}
//-------------------------------------------------------------------------
template <typename T>
std::enable_if_t<std::is_floating_point_v<T>,T>
uniform (void)
{
return uniform<T> (0, 1);
}
///------------------------------------------------------------------------
/// select a value uniformly from the range [lo, hi) using a supplied
/// generator
template <
typename T,
typename GeneratorT,
typename = std::enable_if_t<std::is_integral_v<T>>
>
T
uniform (T lo, T hi, GeneratorT &&gen)
{
return std::uniform_int_distribution<T> { lo, hi } (gen);
}
///------------------------------------------------------------------------
/// 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<T> { lo, hi } (generator ());
return uniform (lo, hi, generator ());
}
///////////////////////////////////////////////////////////////////////////
//-------------------------------------------------------------------------
template <typename T>
std::enable_if_t<std::is_integral_v<T>,T>
uniform (void)