Support random::uniform_from with initializer_list

This commit is contained in:
Nikita Lisitsa 2025-05-14 22:52:47 +03:00
parent 8e89679219
commit d5abbb4762

View file

@ -9,7 +9,7 @@
#include <psemek/util/exception.hpp> #include <psemek/util/exception.hpp>
#include <type_traits> #include <type_traits>
#include <stdexcept> #include <initializer_list>
namespace psemek::random namespace psemek::random
{ {
@ -71,4 +71,10 @@ namespace psemek::random
return *std::next(begin(container), uniform<std::size_t>(rng, 0, size(container) - 1)); return *std::next(begin(container), uniform<std::size_t>(rng, 0, size(container) - 1));
} }
template <typename RNG, typename T>
decltype(auto) uniform_from(RNG && rng, std::initializer_list<T> const & list)
{
return *(list.begin() + uniform<std::size_t>(rng, 0, list.size() - 1));
}
} }