9 : m_blocks(_probs.size()), m_diceroll(0, _probs.size() - 1) {
10 double factor = 1.0 / std::accumulate(_probs.begin(), _probs.end(), 0.0);
12 unsigned int n = _probs.size();
14 vector<double> probs(n);
15 for (
unsigned int i = 0; i < n; i++) probs[i] = _probs[i] * factor * n;
17 deque<unsigned int> small, large;
18 for (
unsigned int i = 0; i < n; i++) {
19 (probs[i] < 1.0 ? small : large).push_back(i);
22 while ((not small.empty()) and (not large.empty())) {
23 unsigned int l = *small.begin();
25 unsigned int g = *large.begin();
28 m_blocks[l].prob = probs[l];
29 m_blocks[l].alias = g;
30 probs[g] = (probs[g] + probs[l]) - 1;
31 (probs[g] < 1.0 ? small : large).push_back(g);
34 for (
unsigned int i: large) m_blocks[i].prob = 1.0;
36 for (
unsigned int i: small) m_blocks[i].prob = 1.0;
41 template<
typename K,
typename V>
45 for (
const auto& it: m) {
46 v.push_back(it.second);
54 for (
const auto& it: m) {
Usage is very simple, construct with a vector of probabilities, then use as a distribution from the s...
static uniform_real_distribution< double > g_coinflip
Time Dependent Person DataType.
AliasDistribution()=delete
MappedAliasDistribution()=default
map< unsigned int, unsigned int > m_translation
vector< V > map_values(const map< K, V > &m)