| 105 | |
| 106 | template <typename T, typename Generator> |
| 107 | T probabilityMapRandomizer(Generator &g, const std::map<T, float> &probabilityMap) |
| 108 | { |
| 109 | if (probabilityMap.empty()) |
| 110 | { |
| 111 | throw std::runtime_error("Called with empty probabilityMap"); |
| 112 | } |
| 113 | float total = 0.0f; |
| 114 | for (auto &p : probabilityMap) |
| 115 | { |
| 116 | total += p.second; |
| 117 | } |
| 118 | std::uniform_real_distribution<float> dist(0, total); |
| 119 | |
| 120 | float val = dist(g); |
| 121 | |
| 122 | // Due to fp precision there's a small chance the total will be slightly more than the max, |
| 123 | // so have a fallback just in case? |
| 124 | T fallback = probabilityMap.begin()->first; |
| 125 | total = 0.0f; |
| 126 | |
| 127 | for (auto &p : probabilityMap) |
| 128 | { |
| 129 | if (val < total + p.second) |
| 130 | { |
| 131 | return p.first; |
| 132 | } |
| 133 | total += p.second; |
| 134 | } |
| 135 | return fallback; |
| 136 | } |
| 137 | |
| 138 | template <typename T, typename Generator> T randBoundsExclusive(Generator &g, T min, T max) |
| 139 | { |
no test coverage detected