random: add unit float and ranged int generator
This commit is contained in:
parent
a90ce0920f
commit
d21b7809dd
32
random.hpp
32
random.hpp
@ -17,10 +17,13 @@
|
|||||||
#ifndef CRUFT_UTIL_RANDOM_HPP
|
#ifndef CRUFT_UTIL_RANDOM_HPP
|
||||||
#define CRUFT_UTIL_RANDOM_HPP
|
#define CRUFT_UTIL_RANDOM_HPP
|
||||||
|
|
||||||
|
#include "coord/traits.hpp"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <random>
|
#include <random>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
|
|
||||||
namespace util::random {
|
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)
|
/// select a value uniformly from the range [lo, hi)
|
||||||
template <typename T>
|
template <typename T>
|
||||||
std::enable_if_t<std::is_integral_v<T>,T>
|
std::enable_if_t<std::is_integral_v<T>,T>
|
||||||
uniform (T lo, T hi)
|
uniform (T lo, T hi)
|
||||||
{
|
{
|
||||||
return std::uniform_int_distribution<T> { lo, hi } (generator ());
|
return uniform (lo, hi, generator ());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////
|
//-------------------------------------------------------------------------
|
||||||
template <typename T>
|
template <typename T>
|
||||||
std::enable_if_t<std::is_integral_v<T>,T>
|
std::enable_if_t<std::is_integral_v<T>,T>
|
||||||
uniform (void)
|
uniform (void)
|
||||||
|
Loading…
Reference in New Issue
Block a user