Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //common_random.h:
- #ifndef COMMON_RANDOM_H
- #define COMMON_RANDOM_H
- #include <random>
- class SingletonGenerator {
- static std::mt19937 mersennetwister;
- public:
- static std::mt19937& get_mt();
- };
- #endif //COMMON_RANDOM_H
- ———————
- //common_random.cpp:
- #include "common_random.h"
- std::mt19937 SingletonGenerator::mersennetwister;
- std::mt19937& SingletonGenerator::get_mt() {
- return mersennetwister;
- }
- ———————
- //foreign_file.h
- #ifndef FOREIGN_FILE_H
- #define FOREIGN_FILE_H
- double getu01_sing();
- #endif //FOREIGN_FILE_H
- ———————
- //foreign_file.cpp
- #include "foreign_file.h"
- #include "common_random.h"
- #include <random>
- double getu01_sing() {
- std::uniform_real_distribution<double> unif_real_01(0.0, 1.0);
- return unif_real_01(SingletonGenerator::get_mt());
- }
- ———————
- //main.cpp
- #include <iostream>
- #include "common_random.h"
- #include "foreign_file.h"
- int main() {
- SingletonGenerator::get_mt().seed(456);
- for (int i = 0; i < 20; ++i) {
- std::cout << i << " " << SingletonGenerator::get_mt()() << std::endl;
- }
- std::cout << "------" << std::endl;
- SingletonGenerator::get_mt().seed(456);
- for (int i = 0; i < 7; ++i) {
- std::cout << i << " " << getu01_sing() << std::endl;
- }
- return 0;
- }
- ———————
- how to build:
- g++ -c common_random.cpp -o common_random.o
- g++ -c foreign_file.cpp -o foreign_file.o
- g++ -c main.cpp -o main.o
- g++ common_random.o foreign_file.o main.o -o example
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement