-
-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Suggestion: Weighted random number generation using maps. #43
Comments
The library it's a simple 'get' method templated "overloads" They should not conflict with each other, in your example it will conflict with Random::get(my_map) which returns random iterator from any container so we need to think how the final end library user interface will look like for the feature |
Do you have any ideas on how can we implement this feature other than implementing it with a different function name? If you don't, are you ok with implementing it with a different name? Also, I realized the template<typename...>
using void_t = void;
template<typename Type, typename = void>
struct is_map : public std::false_type {};
template<typename Type>
struct is_map<Type, void_t<typename Type::key_type, typename Type::mapped_type, typename Type::value_type>> : public std::true_type{}; |
Actually why do you think there is a vectors modifying problem with std::discrete_distribution ? The indexed approach can be used with existing containers while hash-map of weights need to be constructed from its values I would appreciate to see some specific use cases for this Also it can be implemented with existing 'get' method using some explicit types like it done already for common type numbers with a key type: And it would be much better to return iterators instead of value copies to prevent extra copies of a possible user defined heavy types |
My problem is, as I stated before, modifying one requires a modification to another and it feels a bit unsafe. It is not a specific one, but the use case scenario I've had in my mind is that I had a box object in a game and when it got destroyed, it would spit out a random game object based on its weight. Also, there are probably no specific use cases for this method since we can just create a map from two same size vectors, one containing unique elements, one containing weights, and vice versa. But this method, by the design of the maps, ensures that the keys are unique. And you are right, returning iterators instead of value copies makes much more sense. This way, when the map is empty, we don't have to return a new key type, we can just return the end iterator. |
Yeah, are you planning to do a PR for this ? |
By the way, I did a PR #44. In case if you missed it. |
Currently, as far as I'm concerned, the only way to get weighted random number generation is using std::discrete_distribution.
But the problem with that approach is that you have to modify one vector after a change on the other one. So, my idea is using a std::map or std::unordered_map for weighted random numbers by assuming we have a map of pairs, each containing an object and its weight. I have a code snippet that works but, I am not sure whether if it should be implemented to the library yet. And it is also a bit messy because it is my first time using std::enable_if etc.
The text was updated successfully, but these errors were encountered: