random: add container random choice
This commit is contained in:
parent
b0897a5ea7
commit
98705a61c9
15
random.hpp
15
random.hpp
@ -155,4 +155,19 @@ namespace cruft::random {
|
|||||||
std::uniform_int_distribution<size_t> dist (0, N - 1);
|
std::uniform_int_distribution<size_t> dist (0, N - 1);
|
||||||
return t[dist (generator ())];
|
return t[dist (generator ())];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//-------------------------------------------------------------------------
|
||||||
|
template <typename ContainerT>
|
||||||
|
auto
|
||||||
|
choose (ContainerT &data)
|
||||||
|
{
|
||||||
|
if (data.empty ())
|
||||||
|
return data.end ();
|
||||||
|
|
||||||
|
return std::next (
|
||||||
|
data.begin (),
|
||||||
|
uniform (typename ContainerT::size_type {0}, data.size () - 1)
|
||||||
|
);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user