MCPcopy Create free account
hub / github.com/OpenXcom/OpenXcom / choose

Method choose

src/Savegame/WeightedOptions.cpp:32–48  ·  view source on GitHub ↗

* 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. */

Source from the content-addressed store, hash-verified

30 * @return The key of the selected choice.
31 */
32const 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.

Callers 3

chooseRandomRegionMethod · 0.80
chooseRandomMissionMethod · 0.80
generateRaceMethod · 0.80

Calls 1

generateFunction · 0.85

Tested by

no test coverage detected