Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //common_random.h:
  2.  
  3. #ifndef COMMON_RANDOM_H
  4. #define COMMON_RANDOM_H
  5. #include <random>
  6.  
  7. class SingletonGenerator {
  8.   static std::mt19937 mersennetwister;
  9. public:
  10.   static std::mt19937& get_mt();
  11. };
  12.  
  13. #endif //COMMON_RANDOM_H
  14.  
  15. ———————
  16.  
  17. //common_random.cpp:
  18.  
  19. #include "common_random.h"
  20.  
  21. std::mt19937 SingletonGenerator::mersennetwister;
  22.  
  23. std::mt19937& SingletonGenerator::get_mt() {
  24.     return mersennetwister;
  25. }
  26.  
  27. ———————
  28.  
  29. //foreign_file.h
  30.  
  31. #ifndef FOREIGN_FILE_H
  32. #define FOREIGN_FILE_H
  33.  
  34. double getu01_sing();
  35.  
  36. #endif //FOREIGN_FILE_H
  37.  
  38. ———————
  39.  
  40. //foreign_file.cpp
  41.  
  42. #include "foreign_file.h"
  43. #include "common_random.h"
  44. #include <random>
  45.  
  46. double getu01_sing() {
  47.   std::uniform_real_distribution<double> unif_real_01(0.0, 1.0);
  48.   return unif_real_01(SingletonGenerator::get_mt());
  49. }
  50.  
  51. ———————
  52.  
  53.  
  54. //main.cpp
  55.  
  56. #include <iostream>
  57. #include "common_random.h"
  58. #include "foreign_file.h"
  59.  
  60.  
  61.  
  62. int main() {
  63.   SingletonGenerator::get_mt().seed(456);
  64.  
  65.   for (int i = 0; i < 20; ++i) {
  66.     std::cout << i << " " << SingletonGenerator::get_mt()() << std::endl;
  67.   }
  68.  
  69.   std::cout << "------" << std::endl;
  70.   SingletonGenerator::get_mt().seed(456);
  71.  
  72.   for (int i = 0; i < 7; ++i) {
  73.     std::cout << i << " " << getu01_sing() << std::endl;
  74.   }
  75.   return 0;
  76. }
  77.  
  78. ———————
  79.  
  80. how to build:
  81. g++ -c common_random.cpp -o common_random.o
  82. g++ -c foreign_file.cpp -o foreign_file.o
  83. g++ -c main.cpp -o main.o
  84. g++ common_random.o foreign_file.o main.o -o example
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement