random: add an explicit generator parameter for container choice
This commit is contained in:
parent
b8af3d0d9e
commit
831b02c907
16
random.hpp
16
random.hpp
@ -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 ());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user