fork download
  1. // utilities.h
  2. template <typename Generator>
  3. double randomDoubleEngine(Generator& engine, double low_bound, double high_bound )
  4. {
  5. if (low_bound > high_bound){
  6. std::swap(low_bound, high_bound);
  7. }
  8.  
  9. return std::uniform_real_distribution<>( low_bound, high_bound )( engine );
  10. }
  11.  
  12. template < typename Generator = std::mt19937, typename Distribution = std::uniform_real_distribution<double>, typename T = double >
  13. struct randGen
  14. {
  15. randGen(const Generator & eng, T low, T high) : m_engine(eng), m_distribution(low, high) {}
  16. T operator()() { return m_distribution(m_engine); }
  17.  
  18. private:
  19.  
  20. Generator m_engine;
  21. Distribution m_distribution;
  22. };
  23.  
  24. // myclass.cpp
  25. void myclass::doSomething1(param1, param2, ...){
  26.  
  27. std::random_device rd;
  28. static std::mt19937 engine(rd());
  29.  
  30. for (i=0; i < someLimit; i++){
  31. Compute interval [treshmin, threshmax]
  32.  
  33. for(int j=0; j < numIter; j++){
  34. randThreshold = randomDoubleEngine(engine, treshmin, treshmax ); // <------------------
  35. ....
  36. }
  37. }
  38. }
  39.  
  40. void myclass::doSomething2(param1, param2, ...){
  41.  
  42. std::random_device rd;
  43. static std::mt19937 engine(rd());
  44.  
  45. for (i=0; i < someLimit; i++){
  46. Compute interval [treshmin, threshmax]
  47.  
  48. randGen<std::mt19937, std::uniform_real_distribution<double>, double > myRandGen( engine, treshmin, treshmax );
  49.  
  50. for(int j=0; j < numIter; j++){
  51. randThreshold = myRandGen(); // <------------------
  52. ....
  53. }
  54. }
  55. }
  56.  
  57. void myclass::doSomething3(param1, param2, ...){
  58.  
  59. std::random_device rd;
  60. std::mt19937 engine(rd());
  61.  
  62. for (i=0; i < someLimit; i++){
  63. Compute interval [treshmin, threshmax]
  64.  
  65. randGen<std::mt19937, std::uniform_real_distribution<double>, double > myRandGen( engine, treshmin, treshmax );
  66.  
  67. for(int j=0; j < numIter; j++){
  68. randThreshold = myRandGen(); // <------------------
  69. ....
  70. }
  71. }
  72. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'double randomDoubleEngine(Generator&, double, double)':
prog.cpp:6:4: error: 'swap' is not a member of 'std'
prog.cpp:9:10: error: 'uniform_real_distribution' is not a member of 'std'
prog.cpp:9:41: error: expected primary-expression before '>' token
prog.cpp: At global scope:
prog.cpp:12:34: error: expected type-specifier
prog.cpp:12:34: error: expected '>'
prog.cpp:15:34: error: 'T' has not been declared
prog.cpp:15:41: error: 'T' has not been declared
prog.cpp:16:3: error: 'T' does not name a type
prog.cpp:21:3: error: 'Distribution' does not name a type
prog.cpp: In constructor 'randGen<Generator>::randGen(const Generator&, int, int)':
prog.cpp:15:67: error: class 'randGen<Generator>' does not have any field named 'm_distribution'
prog.cpp: At global scope:
prog.cpp:25:7: error: 'myclass' has not been declared
prog.cpp:25:29: error: variable or field 'doSomething1' declared void
prog.cpp:25:29: error: 'param1' was not declared in this scope
prog.cpp:25:37: error: 'param2' was not declared in this scope
prog.cpp:25:45: error: expected primary-expression before '...' token
stdout
Standard output is empty