Add random::uniform_from(container)
This commit is contained in:
parent
ef80db2620
commit
02382155a7
1 changed files with 9 additions and 0 deletions
|
|
@ -7,6 +7,7 @@
|
||||||
#include <psemek/geom/box.hpp>
|
#include <psemek/geom/box.hpp>
|
||||||
|
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
namespace psemek::random
|
namespace psemek::random
|
||||||
{
|
{
|
||||||
|
|
@ -47,4 +48,12 @@ namespace psemek::random
|
||||||
return uniform(rng, T{0}, T{2 * geom::pi});
|
return uniform(rng, T{0}, T{2 * geom::pi});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename RNG, typename Container>
|
||||||
|
auto & uniform_from(RNG && rng, Container & container)
|
||||||
|
{
|
||||||
|
if (container.empty())
|
||||||
|
throw std::runtime_error("cannot sample from empty container");
|
||||||
|
return container[uniform<std::size_t>(rng, 0, container.size() - 1)];
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue