Make random::uniform_from work with raw arrays

This commit is contained in:
Nikita Lisitsa 2023-07-08 11:17:28 +03:00
parent 2ea0427b0f
commit 9e65c02541

View file

@ -62,9 +62,11 @@ namespace psemek::random
template <typename RNG, typename Container>
decltype(auto) uniform_from(RNG && rng, Container && container)
{
if (container.size() == 0)
using std::size;
using std::begin;
if (size(container) == 0)
throw std::runtime_error("cannot sample from empty container");
return *std::next(container.begin(), uniform<std::size_t>(rng, 0, container.size() - 1));
return *std::next(begin(container), uniform<std::size_t>(rng, 0, size(container) - 1));
}
}