Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@deque-blog
Last active April 29, 2017 11:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deque-blog/b193e80e754ad9c888fca8e6cff26b65 to your computer and use it in GitHub Desktop.
Save deque-blog/b193e80e754ad9c888fca8e6cff26b65 to your computer and use it in GitHub Desktop.
template<typename Value>
auto weighted_choice_gen(std::vector<Weighted<Value>> const& weighted_values)
{
auto const& intervals = details::make_intervals(weighted_values);
double sum_weights = intervals.back().first;
return [=](std::mt19937& bit_gen) -> Value
{
std::uniform_real_distribution<double> distribution(0., sum_weights);
return details::search_containing_interval(intervals, distribution(bit_gen));
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment