* Select a random choice from among the contents. * This MUST be called on non-empty objects. * Each time this is called, the returned value can be different. * @return The key of the selected choice. */
| 30 | * @return The key of the selected choice. |
| 31 | */ |
| 32 | const std::string WeightedOptions::choose() const |
| 33 | { |
| 34 | if (_totalWeight == 0) |
| 35 | { |
| 36 | return ""; |
| 37 | } |
| 38 | size_t var = RNG::generate(0, _totalWeight); |
| 39 | std::map<std::string, size_t>::const_iterator ii = _choices.begin(); |
| 40 | for (; ii != _choices.end(); ++ii) |
| 41 | { |
| 42 | if (var <= ii->second) |
| 43 | break; |
| 44 | var -= ii->second; |
| 45 | } |
| 46 | // We always have a valid iterator here. |
| 47 | return ii->first; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Select the most likely option. |
no test coverage detected