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