random: add an explicit generator parameter for container choice

This commit is contained in:
Danny Robson 2019-05-12 14:34:53 +10:00
parent b8af3d0d9e
commit 831b02c907

View File

@ -158,18 +158,28 @@ namespace cruft::random {
//-------------------------------------------------------------------------
template <typename ContainerT>
template <typename ContainerT, typename GeneratorT>
auto
choose (ContainerT &data)
choose (ContainerT &data, GeneratorT &&gen)
{
if (data.empty ())
return data.end ();
auto const offset = uniform (
typename ContainerT::size_type {0},
data.size () - 1
data.size () - 1,
gen
);
return std::next (data.begin (), offset);
}
//-------------------------------------------------------------------------
template <typename ContainerT>
auto
choose (ContainerT &data)
{
return choose (data, generator ());
}
}