Line data Source code
1 : // 2 : // Copyright (c) 2014 Juniper Networks, Inc. All rights reserved. 3 : // Copyright Contributors to the OpenSDN project 4 : // 5 : 6 : #ifndef BASE_RANDOM_GENERATOR_H_ 7 : #define BASE_RANDOM_GENERATOR_H_ 8 : 9 : #include <mutex> 10 : 11 : #include <boost/uuid/random_generator.hpp> 12 : #include <boost/random/mersenne_twister.hpp> 13 : #include <boost/random/uniform_int.hpp> 14 : 15 : class ThreadSafeUuidGenerator { 16 : public: 17 2 : boost::uuids::uuid operator()() { 18 2 : std::scoped_lock lock(mutex_); 19 4 : return rgen_(); 20 2 : } 21 : 22 : private: 23 : boost::uuids::random_generator rgen_; 24 : std::mutex mutex_; 25 : }; 26 : 27 : class UniformInt8RandomGenerator { 28 : public: 29 7 : UniformInt8RandomGenerator(uint8_t min, uint8_t max) : rgen_(min, max) {} 30 : 31 5 : uint8_t operator()() { 32 5 : std::scoped_lock lock(mutex_); 33 10 : return rgen_(rng_); 34 5 : } 35 : 36 : private: 37 : boost::random::mt19937 rng_; 38 : boost::random::uniform_int_distribution<uint8_t> rgen_; 39 : std::mutex mutex_; 40 : }; 41 : 42 : #endif // BASE_RANDOM_GENERATOR_H_