0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
DiscreteRandomGenerator.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
27 #ifndef Common_DiscreteRandomGenerator_h
28 #define Common_DiscreteRandomGenerator_h
29 
30 #include <cassert>
31 #include <memory>
32 #include <random>
33 
34 namespace Hypertable {
35 
46 
47  public:
51 
53  virtual ~DiscreteRandomGenerator() { delete [] m_cmf; };
54 
59  void set_seed(uint32_t s) {
60  m_random_engine.seed(s);
61  }
62 
67  void set_value_count(uint64_t value_count) {
68  m_value_count = value_count;
69  delete [] m_cmf;
70  m_cmf = 0;
71  delete [] m_numbers;
72  m_numbers = 0;
73  }
74 
79  void set_pool_min(uint64_t pool_min) {
80  m_pool_min = pool_min;
81  delete [] m_cmf;
82  m_cmf = 0;
83  delete [] m_numbers;
84  m_numbers = 0;
85  }
86 
91  void set_pool_max(uint64_t pool_max) {
92  m_pool_max = pool_max;
93  delete [] m_cmf;
94  m_cmf = 0;
95  delete [] m_numbers;
96  m_numbers = 0;
97  }
98 
100  virtual uint64_t get_sample();
101 
102  protected:
104  virtual void generate_cmf();
105 
113  virtual double pmf(uint64_t val) { return 1.0 / (double)m_value_count; }
114 
116  std::mt19937 m_random_engine {1};
117 
119  uint64_t m_value_count {};
120 
122  uint64_t m_pool_min {};
123 
125  uint64_t m_pool_max {};
126 
128  uint64_t *m_numbers {};
129 
131  double *m_cmf {};
132  };
133 
134  typedef std::shared_ptr<DiscreteRandomGenerator> DiscreteRandomGeneratorPtr;
135 
138 } // namespace Hypertable
139 
140 #endif // Common_DiscreteRandomGenerator_h
void set_seed(uint32_t s)
Sets the seed for the random number generator.
uint64_t m_pool_min
Lower bound of the range.
uint64_t * m_numbers
Array with the random samples.
uint64_t m_pool_max
Upper bound of the range.
std::mt19937 m_random_engine
The random number generator.
std::shared_ptr< DiscreteRandomGenerator > DiscreteRandomGeneratorPtr
uint64_t m_value_count
Number of values in the range.
Hypertable definitions
void set_pool_min(uint64_t pool_min)
Sets the lowest value of the desired distribution.
virtual double pmf(uint64_t val)
Returns the probability of drawing a specific value from the distribution.
void set_value_count(uint64_t value_count)
Sets the size of the generated range.
Generates samples from a discrete probability distribution in the range [0, max_val] by transforming ...
DiscreteRandomGenerator()
Default constructor; sets up a random number generator with a constant seed value of 1...
virtual uint64_t get_sample()
Returns a random sample from the distribution.
virtual void generate_cmf()
Generate the cumulative mass function for the distribution.
double * m_cmf
The cumulative mass of the distribution.
virtual ~DiscreteRandomGenerator()
Destructor - cleans up allocated resources.
void set_pool_max(uint64_t pool_max)
Sets the highest value of the desired distribution.