Add function to BaseLibrary class to retrieve random value.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-04-19 17:01:33 +02:00
parent 3dcefebee7
commit c6ce5fe8a7
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
1 changed files with 12 additions and 0 deletions

View File

@ -4,6 +4,7 @@
#include <Arbutils/Assert.hpp> #include <Arbutils/Assert.hpp>
#include <Arbutils/Collections/Dictionary.hpp> #include <Arbutils/Collections/Dictionary.hpp>
#include <Arbutils/ConstString.hpp> #include <Arbutils/ConstString.hpp>
#include <Arbutils/Random.hpp>
#include <algorithm> #include <algorithm>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
@ -53,6 +54,17 @@ namespace CreatureLib::Library {
} }
[[nodiscard]] size_t GetCount() const { return _values.Count(); } [[nodiscard]] size_t GetCount() const { return _values.Count(); }
inline const T* GetRandomValue(Arbutils::Random rand = Arbutils::Random()) const {
auto i = rand.Get(_values.Count());
auto& map = _values.GetStdMap();
return std::next(std::begin(map), i)->first;
}
inline const T* GetRandomValue(Arbutils::Random* rand) const {
auto i = rand->Get(_values.Count());
auto& map = _values.GetStdMap();
return std::next(std::begin(map), i)->first;
}
}; };
} }