Graph C++ Library
 All Classes Namespaces Files Functions Variables Typedefs Groups Pages
random_number_generators.hpp
Go to the documentation of this file.
1 
8 #ifndef RANDOM_NUMBER_GENERATORS_HPP_INCLUDED
9 #define RANDOM_NUMBER_GENERATORS_HPP_INCLUDED
10 
11 // Standard Library
12 #include <cmath> // pow
13 #include <cstdlib> // abort
14 #include <limits> //
15 #include <random> // mt19937, uniform_real_distribution, normal_distribution
16 #include <vector>
17 
18 
20 namespace rng
21 {
22 
34  // ==============================================================================================
44  double powerlaw_rand(std::vector<double> &param, std::mt19937& _engine)
45  {
46  std::uniform_real_distribution<double> uniform_01;
47  return param[1] * std::pow(1-( uniform_01(_engine) ),-1./(param[0]-1));
48  }
49 
50  // ==============================================================================================
60  double powerlaw_m1(std::vector<double> &param)
61  {
62  if(param[0] > 2)
63  {
64  return ( ( param[0]-1 ) * param[1] ) / ( param[0]-2 );
65  }
66  else
67  {
68  return std::numeric_limits<double>::infinity();
69  std::cout << "First moment of power-law distribution infinite (gamma < 2)." << std::endl;
70  // abort();
71  }
72  }
73 
74  // ==============================================================================================
85  double powerlaw_hardcutoff_rand(std::vector<double> &param, std::mt19937& _engine)
86  {
87  std::uniform_real_distribution<double> uniform_01;
88  return param[1] * std::pow(1-( uniform_01(_engine) * (1-std::pow(param[2]/param[1],(1.-param[0])))),-1./(param[0]-1));
89  }
90 
91  // ==============================================================================================
102  double powerlaw_hardcutoff_m1(std::vector<double> &param)
103  {
104  return ( ( param[0]-1 ) * param[1] * ( 1 - std::pow(param[2]/param[1],2-param[0]) ) ) / ( ( param[0]-2 ) * ( 1 - std::pow(param[2]/param[1],1-param[0]) ) );
105  }
106 
107  // ==============================================================================================
115  double exponential_rand(std::vector<double> &param, std::mt19937& _engine)
116  {
117  std::uniform_real_distribution<double> uniform_01;
118  return -param[0] * std::log(1-uniform_01(_engine));
119  }
120 
121  // ==============================================================================================
128  double exponential_m1(double param)
129  {
130  return param;
131  }
132 
133  // ==============================================================================================
142  double gamma_rand(std::vector<double> &param, std::mt19937& _engine)
143  {
144  std::uniform_real_distribution<double> uniform_01;
145  std::normal_distribution<double> normal_01(0.0,1.0);
146  double d = param[0] - 1./3.;
147  double lhs = 5.;
148  double rhs = 1.;
149  double x,v;
150  while( lhs > rhs )
151  {
152  lhs = std::log( uniform_01(_engine) );
153  x = normal_01(_engine);
154  v = std::pow(1+x/(std::sqrt(9.*d)),3);
155  rhs = std::pow(x,2)/2. + d - d*v + d*std::log(v);
156  }
157  return param[1]*d*v;
158  }
159 
160  // ==============================================================================================
169  double gamma_m1(std::vector<double> param)
170  {
171  return param[0]*param[1];
172  }
173  // End group RandomNumberGenerators
175 
176 } // End of namespace rng.
177 
178 
179 
180 #endif // RANDOM_NUMBER_GENERATORS_HPP_INCLUDED
double powerlaw_hardcutoff_rand(std::vector< double > &param, std::mt19937 &_engine)
Generates a random real number following a power-law distribution of the form with (i...
Definition: random_number_generators.hpp:85
double powerlaw_m1(std::vector< double > &param)
Returns the first moment of a power-law distribution of the form with .
Definition: random_number_generators.hpp:60
double powerlaw_rand(std::vector< double > &param, std::mt19937 &_engine)
Generates a random real number following a power-law distribution of the form with ...
Definition: random_number_generators.hpp:44
double gamma_m1(std::vector< double > param)
Returns the first moment of a gamma distribution of the form with .
Definition: random_number_generators.hpp:169
double powerlaw_hardcutoff_m1(std::vector< double > &param)
Returns the first moment of a power-law distribution of the form with (i.e., hard cut-off)...
Definition: random_number_generators.hpp:102
double exponential_rand(std::vector< double > &param, std::mt19937 &_engine)
Generates a random real number following an exponential distribution of the form with ...
Definition: random_number_generators.hpp:115
double gamma_rand(std::vector< double > &param, std::mt19937 &_engine)
Generates a random real number following a gamma distribution of the form with . ...
Definition: random_number_generators.hpp:142
double exponential_m1(double param)
Returns the first moment of an exponential distribution of the form with .
Definition: random_number_generators.hpp:128