28 explicit Random(
unsigned long seed = std::random_device()()) : std::mt19937(seed) {}
53 std::vector<typename std::iterator_traits<Iter>::value_type>
uniformSample(Iter poolBegin, Iter poolEnd,
size_t n) {
54 if (poolBegin == poolEnd) {
58 std::vector<typename std::iterator_traits<Iter>::value_type> result;
62 while (result.size() < n) {
63 result.insert(std::end(result), poolBegin, poolEnd);
67 if (result.size() > n) {
69 size_t extra = result.size() - n;
70 std::shuffle(std::end(result) - extra, std::end(result), *
this);
77 std::shuffle(std::begin(result), std::end(result), *
this);
91 std::set<size_t> allAllowed;
92 for (
size_t i = min; i <= max; ++i) {
95 return uniformSample(allAllowed.begin(), allAllowed.end(), n);
106 template <
class Container>
108 std::uniform_int_distribution<size_t> distr(0ul, pool.size() - 1ul);
109 size_t pos = distr(*
this);
111 auto it = std::begin(pool);
112 std::advance(it, pos);
126 size_t size = std::min(n, pool.size());
129 std::vector<const T *> pointerVec;
130 pointerVec.reserve(pool.size());
131 for (
const T &element : pool) {
132 pointerVec.push_back(&element);
134 std::shuffle(pointerVec.begin(), pointerVec.end(), *
this);
138 for (
size_t i = 0; i < size; ++i) {
139 result.insert(*pointerVec[i]);
Class for random algorithms.
Definition: Random.h:22
Random(const Random &other)=delete
Class should not be copied constructed.
Random(unsigned long seed=std::random_device()())
Constructor.
Definition: Random.h:28
std::set< T > randomSubset(std::set< T > pool, size_t n)
Pick up to n random elements from the set.
Definition: Random.h:125
Random & operator=(const Random &other)=delete
Class should not be copied assigned.
auto pickRandom(const Container &pool)
Get a uniformly randomly selected object from the given container.
Definition: Random.h:107
std::vector< size_t > uniformSample(size_t min, size_t max, size_t n)
Sample n points from the set {min;min+1;...;max}.
Definition: Random.h:90
std::vector< typename std::iterator_traits< Iter >::value_type > uniformSample(Iter poolBegin, Iter poolEnd, size_t n)
Sample n points from the pool.
Definition: Random.h:53
static void exception(const Exception e)
Handle an exception derived by std::exception.
Definition: ExceptionHandler.h:63
This is the main namespace of AutoPas.
Definition: AutoPasDecl.h:32