1 | /// @file |
2 | /// @brief randomness |
3 | |
4 | #ifndef RANDOM_HH |
5 | #define RANDOM_HH |
6 | |
7 | #include <iterator> |
8 | #include <random> |
9 | #include <stdexcept> |
10 | #include <utility> |
11 | |
12 | namespace smith { |
13 | extern std::mt19937_64 rng; |
14 | } |
15 | |
16 | template <typename T> T &random_pick(std::vector<T> &container) { |
17 | if (!container.size()) |
18 | throw std::runtime_error("No candidates available" ); |
19 | |
20 | std::uniform_int_distribution<int> pick(0, container.size() - 1); |
21 | return container[pick(smith::rng)]; |
22 | } |
23 | |
24 | template <typename I> I random_pick(I beg, I end) { |
25 | if (beg == end) |
26 | throw std::runtime_error("No candidates available" ); |
27 | |
28 | std::uniform_int_distribution<> pick(0, std::distance(beg, end) - 1); |
29 | std::advance(beg, pick(smith::rng)); |
30 | return beg; |
31 | } |
32 | |
33 | template <typename I> I random_pick(std::pair<I, I> iters) { |
34 | return random_pick(iters.first, iters.second); |
35 | } |
36 | |
37 | int d6(), d9(), d12(), d20(), d42(), d100(); |
38 | |
39 | #endif |
40 | |