Stride Reference Manual  1.0
AliasDistribution.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <vector>
4 #include <map>
5 #include <deque>
6 #include <random>
7 
8 namespace stride {
9 namespace util {
10 
11 using namespace std;
12 
13 struct AliasBlock {
14  double prob;
15  unsigned int alias;
16 };
17 
21 public:
28  AliasDistribution(const vector<double>& probs);
29 
30  AliasDistribution() = delete;
31 
36  template<typename RNG>
37  unsigned int operator()(RNG& gen) {
38  unsigned int i = m_diceroll(gen);
39  double c = g_coinflip(gen);
40  return c < m_blocks[i].prob ? i : m_blocks[i].alias;
41  }
42 
43 protected:
44  vector<AliasBlock> m_blocks;
45  uniform_int_distribution<unsigned int> m_diceroll;
46  static uniform_real_distribution<double> g_coinflip;
47 };
48 
49 
51 public:
52  MappedAliasDistribution(const map<unsigned int, double>& m);
53 
54  MappedAliasDistribution() = default;
55 
60  template<typename RNG>
61  unsigned int operator()(RNG& gen) {
62  unsigned int i = AliasDistribution::operator()(gen);
63  return m_translation[i];
64  }
65 
66 private:
67  map<unsigned int, unsigned int> m_translation;
68 };
69 
70 }
71 }
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.
Definition: NoBehaviour.h:17
unsigned int operator()(RNG &gen)
uniform_int_distribution< unsigned int > m_diceroll
STL namespace.
map< unsigned int, unsigned int > m_translation