diff --git a/src/bin/perfdhcp/test_control.cc b/src/bin/perfdhcp/test_control.cc index fa4c9423e6..fe14ddb65b 100644 --- a/src/bin/perfdhcp/test_control.cc +++ b/src/bin/perfdhcp/test_control.cc @@ -361,7 +361,7 @@ TestControl::generateMacAddress(uint8_t& randomized) { const CommandOptions::MacAddrsVector& macs = options_.getMacsFromFile(); // if we are using the -M option return a random one from the list... if (macs.size() > 0) { - uint16_t r = number_generator_(); + uint16_t r = random_generator_->generate(); if (r >= macs.size()) { r = 0; } @@ -417,7 +417,7 @@ TestControl::generateDuid(uint8_t& randomized) { const CommandOptions::MacAddrsVector& macs = options_.getMacsFromFile(); // pick a random mac address if we are using option -M.. if (macs.size() > 0) { - uint16_t r = number_generator_(); + uint16_t r = random_generator_->generate(); if (r >= macs.size()) { r = 0; } @@ -1106,10 +1106,12 @@ TestControl::reset() { TestControl::TestControl(CommandOptions& options, BasePerfSocket &socket) : exit_time_(not_a_date_time), - number_generator_(0, options.getMacsFromFile().size()), socket_(socket), receiver_(socket, options.isSingleThreaded(), options.getIpVersion()), stats_mgr_(options), + random_generator_(NumberGeneratorPtr( + new RandomGenerator(0, options.getMacsFromFile().size()) + )), options_(options) { // Reset singleton state before test starts. diff --git a/src/bin/perfdhcp/test_control.h b/src/bin/perfdhcp/test_control.h index eda953f8aa..650d43eb75 100644 --- a/src/bin/perfdhcp/test_control.h +++ b/src/bin/perfdhcp/test_control.h @@ -13,7 +13,6 @@ #include #include #include -#include #include #include @@ -24,6 +23,8 @@ #include #include #include +#include +#include #include #include @@ -183,6 +184,33 @@ public: uint32_t range_; ///< Number of unique numbers generated. }; + /// \brief Random numbers generator class. The generated numbers + /// are uniformly distributed in the range of [min, max]. + class RandomGenerator : public NumberGenerator { + public: + /// \brief Constructor. + /// + /// \param min minimum number generated. + /// \param max maximum number generated. + RandomGenerator(uint32_t min, uint32_t max) : + NumberGenerator(), + distribution(min, max) { + // Initialize the randomness source with the current time. + randomnessGenerator.seed(time(NULL)); + } + + /// \brief Generate number in range of [min, max]. + /// + /// \return generated number. + virtual uint32_t generate() { + return distribution(randomnessGenerator); + } + + private: + boost::random::uniform_int_distribution<> distribution; + boost::random::mt19937 randomnessGenerator; + }; + /// \brief Length of the Ethernet HW address (MAC) in bytes. /// /// \todo Make this variable length as there are cases when HW @@ -347,9 +375,6 @@ public: // solution is to make this class friend of test class but this is not // what's followed in other classes. protected: - /// Generate uniformly distributed integers in range of [min, max] - UniformRandomIntegerGenerator number_generator_; - /// \brief Creates DHCPREQUEST from a DHCPACK message. /// /// @param msg_type the message type to be created (DHCPREQUEST or DHCPRELEASE) @@ -1078,6 +1103,10 @@ protected: /// \brief Storage for reply messages. PacketStorage reply_storage_; + /// \brief Generate uniformly distributed integers in range of + /// [min, max]. + NumberGeneratorPtr random_generator_; + /// \brief Transaction id generator. NumberGeneratorPtr transid_gen_;