2011-09-13 15:13:55 +10:00
|
|
|
/*
|
2018-08-04 15:14:06 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2011-09-13 15:13:55 +10:00
|
|
|
*
|
2019-02-21 20:53:32 +11:00
|
|
|
* Copyright 2016-2019 Danny Robson <danny@nerdcruft.net>
|
2011-09-13 15:13:55 +10:00
|
|
|
*/
|
|
|
|
|
2018-11-19 15:36:29 +11:00
|
|
|
#pragma once
|
2011-09-13 15:13:55 +10:00
|
|
|
|
2018-05-30 14:33:17 +10:00
|
|
|
#include "coord/traits.hpp"
|
|
|
|
|
2017-11-22 17:03:00 +11:00
|
|
|
#include <algorithm>
|
2018-05-30 14:32:39 +10:00
|
|
|
#include <array>
|
2016-11-17 18:25:46 +11:00
|
|
|
#include <random>
|
2017-09-21 15:58:26 +10:00
|
|
|
#include <limits>
|
2018-05-30 14:33:17 +10:00
|
|
|
#include <type_traits>
|
2012-01-04 17:03:27 +11:00
|
|
|
|
2019-06-18 15:10:05 +10:00
|
|
|
#include <chrono>
|
|
|
|
|
2011-09-13 15:13:55 +10:00
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
namespace cruft::random {
|
2019-02-21 20:53:32 +11:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
/// Initialise and return a generator using random_device.
|
2018-11-19 15:36:29 +11:00
|
|
|
template <typename GeneratorT>
|
2019-02-21 20:53:32 +11:00
|
|
|
GeneratorT
|
|
|
|
initialise (void)
|
|
|
|
{
|
|
|
|
// Approximate the state size of the generator by its size in bytes.
|
2019-06-18 15:10:05 +10:00
|
|
|
std::array<unsigned,sizeof (GeneratorT) / sizeof (unsigned) + 1 + 1> seed;
|
|
|
|
std::generate_n (
|
|
|
|
seed.begin (),
|
|
|
|
seed.size () - 1,
|
|
|
|
std::random_device ()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Append the current time with the highest resolution we have
|
|
|
|
// available. This defends against instances where the implementation
|
|
|
|
// of std::random_device is actually deterministic.
|
|
|
|
seed[seed.size () - 1] = static_cast<unsigned> (
|
|
|
|
std::chrono::high_resolution_clock::now ().time_since_epoch ().count ()
|
|
|
|
);
|
2018-11-19 15:36:29 +11:00
|
|
|
|
2019-02-21 20:53:32 +11:00
|
|
|
std::seed_seq seq (seed.begin (), seed.end ());
|
|
|
|
return GeneratorT (seq);
|
|
|
|
}
|
2018-05-30 14:32:39 +10:00
|
|
|
|
2018-11-19 15:36:29 +11:00
|
|
|
|
2017-08-28 14:12:36 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2018-11-19 15:36:29 +11:00
|
|
|
/// Returns a correctly pre-initialised reference to a thread-local
|
|
|
|
/// generator of an unspecified (but not entirely useless) type.
|
|
|
|
///
|
|
|
|
/// ie, not LCG.
|
2017-09-21 15:58:02 +10:00
|
|
|
inline auto&
|
2017-08-28 14:12:36 +10:00
|
|
|
generator (void)
|
2016-11-17 18:25:46 +11:00
|
|
|
{
|
2019-02-09 14:57:22 +11:00
|
|
|
using generator_t = std::mt19937_64;
|
2019-02-21 20:53:32 +11:00
|
|
|
static thread_local auto gen = initialise<generator_t> ();
|
2016-11-17 18:25:46 +11:00
|
|
|
return gen;
|
2012-01-04 17:03:27 +11:00
|
|
|
}
|
|
|
|
|
2013-08-06 12:24:00 +10:00
|
|
|
|
2017-08-28 14:12:36 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2018-11-19 15:36:29 +11:00
|
|
|
/// A convenience typedef that selects between
|
|
|
|
/// std::uniform_real_distribution and std::uniform_int_distribution
|
|
|
|
/// depending on the supplied value type.
|
|
|
|
template <typename ValueT>
|
|
|
|
using uniform_distribution = std::conditional_t<
|
|
|
|
std::is_floating_point<ValueT>::value,
|
|
|
|
std::uniform_real_distribution<ValueT>,
|
|
|
|
std::uniform_int_distribution<ValueT>
|
|
|
|
>;
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
/// Returns a value chosen uniformly at random the supplied range.
|
|
|
|
///
|
|
|
|
/// This is primarily a convenience helper around the uniform_distribution
|
2019-04-11 11:49:16 +10:00
|
|
|
/// type. As such, the interval is the same as the std library; ie, closed
|
|
|
|
/// for integers, half-open for reals.
|
2018-11-19 15:36:29 +11:00
|
|
|
template <typename ValueT, typename GeneratorT>
|
2018-11-28 15:24:01 +11:00
|
|
|
decltype(auto)
|
2018-11-19 15:36:29 +11:00
|
|
|
uniform (ValueT lo, ValueT hi, GeneratorT &&gen)
|
2016-11-17 18:25:46 +11:00
|
|
|
{
|
2018-11-28 15:24:01 +11:00
|
|
|
return uniform_distribution<ValueT> { lo, hi } (
|
|
|
|
std::forward<GeneratorT> (gen)
|
|
|
|
);
|
2012-01-04 17:03:27 +11:00
|
|
|
}
|
2017-11-22 17:03:00 +11:00
|
|
|
|
|
|
|
|
2018-11-19 15:36:29 +11:00
|
|
|
///------------------------------------------------------------------------
|
|
|
|
/// Return a value uniformly random chosen value between lo and hi.
|
2018-11-28 15:24:01 +11:00
|
|
|
///
|
|
|
|
/// Interval bounds are treated as per the standard Generator
|
|
|
|
/// implementations; ie, inclusive for integers, exclusive upper for reals.
|
2018-05-30 14:33:17 +10:00
|
|
|
template <typename T>
|
2018-11-28 15:24:01 +11:00
|
|
|
decltype(auto)
|
2018-11-19 15:36:29 +11:00
|
|
|
uniform (T lo, T hi)
|
2018-05-30 14:33:17 +10:00
|
|
|
{
|
2018-11-19 15:36:29 +11:00
|
|
|
return uniform<T> (lo, hi, generator ());
|
2018-05-30 14:33:17 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-13 16:15:24 +10:00
|
|
|
///------------------------------------------------------------------------
|
|
|
|
/// Return a value uniformly chosen between 0 and the given value.
|
|
|
|
///
|
|
|
|
/// Interval bounds are treated as per the standard Generator
|
|
|
|
/// implementations; ie, inclusive for integers, exclusive upper for reals.
|
|
|
|
template <typename T>
|
|
|
|
decltype(auto)
|
|
|
|
uniform (T hi)
|
|
|
|
{
|
|
|
|
return uniform<T> (T{0}, hi);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-05-30 14:33:17 +10:00
|
|
|
///------------------------------------------------------------------------
|
2018-11-19 15:36:29 +11:00
|
|
|
/// Return a uniformly random value chosen on the interval [0,1)
|
2018-05-30 14:33:17 +10:00
|
|
|
template <
|
2018-11-19 15:36:29 +11:00
|
|
|
typename ValueT,
|
2018-05-30 14:33:17 +10:00
|
|
|
typename GeneratorT,
|
2018-11-19 15:36:29 +11:00
|
|
|
typename = std::enable_if_t<std::is_floating_point_v<ValueT>>
|
2018-05-30 14:33:17 +10:00
|
|
|
>
|
2018-11-28 15:24:01 +11:00
|
|
|
decltype(auto)
|
|
|
|
uniform (GeneratorT &&gen)
|
2018-05-30 14:33:17 +10:00
|
|
|
{
|
2018-11-19 15:36:29 +11:00
|
|
|
return uniform<ValueT> (ValueT{0}, ValueT{1}, std::forward<GeneratorT> (gen));
|
2018-05-30 14:33:17 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///------------------------------------------------------------------------
|
2018-11-19 15:36:29 +11:00
|
|
|
/// Return a uniformly random chosen value on the interval [0.f, 1.f)
|
|
|
|
template <
|
|
|
|
typename ValueT,
|
|
|
|
typename = std::enable_if_t<std::is_floating_point_v<ValueT>>
|
|
|
|
>
|
2018-11-28 15:24:01 +11:00
|
|
|
decltype(auto)
|
|
|
|
uniform (void)
|
2017-11-22 17:03:00 +11:00
|
|
|
{
|
2018-11-19 15:36:29 +11:00
|
|
|
return uniform<ValueT> (ValueT{0}, ValueT{1}, generator ());
|
2017-08-28 14:12:36 +10:00
|
|
|
}
|
|
|
|
|
2017-11-22 17:03:00 +11:00
|
|
|
|
2018-11-19 15:36:29 +11:00
|
|
|
///------------------------------------------------------------------------
|
|
|
|
/// Return a uniformly random chosen value on the interval [0, 1]
|
2017-09-21 15:58:26 +10:00
|
|
|
template <typename T>
|
|
|
|
std::enable_if_t<std::is_integral_v<T>,T>
|
|
|
|
uniform (void)
|
|
|
|
{
|
2018-11-19 15:36:29 +11:00
|
|
|
return uniform<T> (
|
2017-09-21 15:58:26 +10:00
|
|
|
std::numeric_limits<T>::min (),
|
|
|
|
std::numeric_limits<T>::max ()
|
|
|
|
);
|
|
|
|
}
|
2017-11-22 17:03:00 +11:00
|
|
|
|
|
|
|
|
2018-11-19 15:36:29 +11:00
|
|
|
///------------------------------------------------------------------------
|
|
|
|
/// Returns a uniformly random initialised coordinate type by value.
|
2018-05-30 14:33:31 +10:00
|
|
|
template <
|
2018-11-19 15:36:29 +11:00
|
|
|
typename ValueT,
|
2018-05-30 14:33:31 +10:00
|
|
|
typename = std::enable_if_t<
|
2018-11-19 15:36:29 +11:00
|
|
|
is_coord_v<ValueT> && std::is_floating_point_v<typename ValueT::value_type>
|
2018-05-30 14:33:31 +10:00
|
|
|
>
|
|
|
|
>
|
2018-11-19 15:36:29 +11:00
|
|
|
ValueT
|
2018-05-30 14:33:31 +10:00
|
|
|
uniform (void)
|
|
|
|
{
|
2019-04-10 14:06:35 +10:00
|
|
|
ValueT res;
|
|
|
|
for (auto &v: res)
|
|
|
|
v = uniform<typename ValueT::value_type> ();
|
2018-05-30 14:33:31 +10:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-28 14:12:36 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2019-05-26 10:11:01 +10:00
|
|
|
/// Choose a value at random from an array
|
|
|
|
///
|
|
|
|
/// We return pointers (rather than values) so that we can return values
|
|
|
|
/// for empty containers without invalid dereferences.
|
|
|
|
///
|
|
|
|
/// \return A pointer to the chosen value.
|
2017-08-28 14:12:36 +10:00
|
|
|
template <typename T, size_t N>
|
2019-05-26 10:11:01 +10:00
|
|
|
T*
|
2017-08-28 14:12:36 +10:00
|
|
|
choose (T (&t)[N])
|
2016-11-17 18:25:46 +11:00
|
|
|
{
|
2017-08-28 14:12:36 +10:00
|
|
|
std::uniform_int_distribution<size_t> dist (0, N - 1);
|
2019-05-26 10:11:01 +10:00
|
|
|
return &t[dist (generator ())];
|
2017-11-22 17:03:00 +11:00
|
|
|
}
|
2019-05-12 11:04:36 +10:00
|
|
|
|
|
|
|
|
2019-05-26 10:11:01 +10:00
|
|
|
///------------------------------------------------------------------------
|
|
|
|
/// Choose a value at random from a container.
|
|
|
|
///
|
|
|
|
/// We return iterators (rather than values) so that we can return values
|
|
|
|
/// for empty containers without invalid dereferences.
|
|
|
|
//
|
|
|
|
/// \return An iterator to the chosen value.
|
2019-05-12 14:34:53 +10:00
|
|
|
template <typename ContainerT, typename GeneratorT>
|
2019-05-26 10:11:01 +10:00
|
|
|
typename ContainerT::iterator
|
2019-05-12 14:34:53 +10:00
|
|
|
choose (ContainerT &data, GeneratorT &&gen)
|
2019-05-12 11:04:36 +10:00
|
|
|
{
|
|
|
|
if (data.empty ())
|
|
|
|
return data.end ();
|
|
|
|
|
2019-05-12 11:31:29 +10:00
|
|
|
auto const offset = uniform (
|
|
|
|
typename ContainerT::size_type {0},
|
2019-05-12 14:34:53 +10:00
|
|
|
data.size () - 1,
|
|
|
|
gen
|
2019-05-12 11:04:36 +10:00
|
|
|
);
|
2019-05-12 11:31:29 +10:00
|
|
|
|
|
|
|
return std::next (data.begin (), offset);
|
2019-05-12 11:04:36 +10:00
|
|
|
}
|
2019-05-12 14:34:53 +10:00
|
|
|
|
|
|
|
|
2019-05-26 10:11:01 +10:00
|
|
|
///------------------------------------------------------------------------
|
|
|
|
/// Choose a value at random from a container.
|
|
|
|
///
|
|
|
|
/// We return iterators (rather than values) so that we can return values
|
|
|
|
/// for empty containers without invalid dereferences.
|
|
|
|
///
|
|
|
|
/// \return An iterator to the chosen value.
|
2019-05-12 14:34:53 +10:00
|
|
|
template <typename ContainerT>
|
2019-05-26 10:11:01 +10:00
|
|
|
typename ContainerT::iterator
|
2019-05-12 14:34:53 +10:00
|
|
|
choose (ContainerT &data)
|
|
|
|
{
|
|
|
|
return choose (data, generator ());
|
|
|
|
}
|
2019-05-12 11:31:29 +10:00
|
|
|
}
|