random_number_generator.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. // $Id$
  15. #ifndef __NSAS_RANDOM_NUMBER_GENERATOR_H
  16. #define __NSAS_RANDOM_NUMBER_GENERATOR_H
  17. #include <cmath>
  18. #include <numeric>
  19. #include <boost/random/mersenne_twister.hpp>
  20. #include <boost/random/uniform_int.hpp>
  21. #include <boost/random/uniform_real.hpp>
  22. #include <boost/random/variate_generator.hpp>
  23. namespace isc {
  24. namespace nsas {
  25. /// \brief Uniform random integer generator
  26. ///
  27. /// Generate uniformly distributed integers in range of [min, max]
  28. class UniformRandomIntegerGenerator{
  29. public:
  30. /// \brief Constructor
  31. ///
  32. /// \param min The minimum number in the range
  33. /// \param max The maximum number in the range
  34. UniformRandomIntegerGenerator(int min, int max):
  35. min_(min), max_(max), dist_(min, max), generator_(rng_, dist_)
  36. {
  37. // Init with the current time
  38. rng_.seed(time(NULL));
  39. }
  40. /// \brief Generate uniformly distributed integer
  41. int operator()() { return generator_(); }
  42. private:
  43. /// Hide default and copy constructor
  44. UniformRandomIntegerGenerator();///< Default constructor
  45. UniformRandomIntegerGenerator(const UniformRandomIntegerGenerator&); ///< Copy constructor
  46. int min_; ///< The minimum integer that can generate
  47. int max_; ///< The maximum integer that can generate
  48. boost::uniform_int<> dist_; ///< Distribute uniformly.
  49. boost::mt19937 rng_; ///< Mersenne Twister: A 623-dimensionally equidistributed uniform pseudo-random number generator
  50. boost::variate_generator<boost::mt19937&, boost::uniform_int<> > generator_; ///< Uniform generator
  51. };
  52. /// \brief Weighted random integer generator
  53. ///
  54. /// Generate random integers according different probabilities
  55. class WeightedRandomIntegerGenerator {
  56. public:
  57. /// \brief Constructor
  58. ///
  59. /// \param probabilities The probabies for all the integers, the probability must be
  60. /// between 0 and 1.0, the sum of probabilities must be equal to 1.
  61. /// For example, if the probabilities contains the following values:
  62. /// 0.5 0.3 0.2, the 1st integer will be generated more frequently than the
  63. /// other integers and the probability is proportional to its value.
  64. /// \param min The minimum integer that generated, other integers will be
  65. /// min, min + 1, ..., min + probabilities.size() - 1
  66. WeightedRandomIntegerGenerator(const std::vector<double>& probabilities,
  67. size_t min = 0):
  68. dist_(0, 1.0), uniform_real_gen_(rng_, dist_), min_(min)
  69. {
  70. // The probabilities must be valid
  71. assert(isProbabilitiesValid(probabilities));
  72. // Calculate the partial sum of probabilities
  73. std::partial_sum(probabilities.begin(), probabilities.end(),
  74. std::back_inserter(cumulative_));
  75. // Init with the current time
  76. rng_.seed(time(NULL));
  77. }
  78. /// \brief Default constructor
  79. ///
  80. WeightedRandomIntegerGenerator():
  81. dist_(0, 1.0), uniform_real_gen_(rng_, dist_), min_(0)
  82. {
  83. }
  84. /// \brief Reset the probabilities
  85. ///
  86. /// Change the weights of each integers
  87. /// \param probabilities The probabies for all the integers
  88. /// \param min The minimum integer that generated
  89. void reset(const std::vector<double>& probabilities, size_t min = 0)
  90. {
  91. // The probabilities must be valid
  92. assert(isProbabilitiesValid(probabilities));
  93. // Reset the cumulative sum
  94. cumulative_.clear();
  95. // Calculate the partial sum of probabilities
  96. std::partial_sum(probabilities.begin(), probabilities.end(),
  97. std::back_inserter(cumulative_));
  98. // Reset the minimum integer
  99. min_ = min;
  100. // Reset the random number generator
  101. rng_.seed(time(NULL));
  102. }
  103. /// \brief Generate weighted random integer
  104. size_t operator()()
  105. {
  106. return std::lower_bound(cumulative_.begin(), cumulative_.end(), uniform_real_gen_())
  107. - cumulative_.begin() + min_;
  108. }
  109. /// \brief Destroctor
  110. ~WeightedRandomIntegerGenerator()
  111. {
  112. }
  113. private:
  114. /// \brief Check the validation of probabilities vector
  115. ///
  116. /// The probability must be in range of [0, 1.0] and the sum must be equal to 1.0
  117. /// Empty probabilities is also valid.
  118. bool isProbabilitiesValid(const std::vector<double>& probabilities) const
  119. {
  120. typedef std::vector<double>::const_iterator Iterator;
  121. double sum = probabilities.empty() ? 1.0 : 0.0;
  122. for(Iterator it = probabilities.begin(); it != probabilities.end(); ++it){
  123. //The probability must be in [0, 1.0]
  124. if(*it < 0.0 || *it > 1.0) {
  125. return false;
  126. }
  127. sum += *it;
  128. }
  129. double epsilon = 0.0001;
  130. // The sum must be equal to 1
  131. return fabs(sum - 1.0) < epsilon;
  132. }
  133. // Shortcut typedefs
  134. typedef boost::variate_generator<boost::mt19937&, boost::uniform_real<> > UniformRealGenerator;
  135. std::vector<double> cumulative_; ///< The partial sum of the probabilities
  136. boost::mt19937 rng_; ///< Mersenne Twister: A 623-dimensionally equidistributed uniform pseudo-random number generator
  137. boost::uniform_real<> dist_; ///< Uniformly distributed real numbers
  138. UniformRealGenerator uniform_real_gen_; ///< Uniformly distributed random real numbers generator
  139. size_t min_; ///< The minimum integer that will be generated
  140. };
  141. } // namespace dns
  142. } // namespace isc
  143. #endif//__NSAS_RANDOM_NUMBER_GENERATOR_H