random_number_generator.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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, int min = 0):
  67. dist_(0, 1.0), uniform_real_gen_(rng_, dist_), min_(min)
  68. {
  69. // The probabilities must be valid
  70. assert(isProbabilitiesValid(probabilities));
  71. // Calculate the partial sum of probabilities
  72. std::partial_sum(probabilities.begin(), probabilities.end(),
  73. std::back_inserter(cumulative_));
  74. // Init with the current time
  75. rng_.seed(time(NULL));
  76. }
  77. /// \brief Default constructor
  78. ///
  79. WeightedRandomIntegerGenerator():
  80. dist_(0, 1.0), uniform_real_gen_(rng_, dist_), min_(0)
  81. {
  82. }
  83. /// \brief Reset the probabilities
  84. ///
  85. /// Change the weights of each integers
  86. /// \param probabilities The probabies for all the integers
  87. /// \param min The minimum integer that generated
  88. void reset(const std::vector<double>& probabilities, int min = 0)
  89. {
  90. // The probabilities must be valid
  91. assert(isProbabilitiesValid(probabilities));
  92. // Reset the cumulative sum
  93. cumulative_.clear();
  94. // Calculate the partial sum of probabilities
  95. std::partial_sum(probabilities.begin(), probabilities.end(),
  96. std::back_inserter(cumulative_));
  97. // Reset the minimum integer
  98. min_ = min;
  99. // Reset the random number generator
  100. rng_.seed(time(NULL));
  101. }
  102. /// \brief Generate weighted random integer
  103. int operator()()
  104. {
  105. return std::lower_bound(cumulative_.begin(), cumulative_.end(), uniform_real_gen_())
  106. - cumulative_.begin() + min_;
  107. }
  108. /// \brief Destroctor
  109. ~WeightedRandomIntegerGenerator()
  110. {
  111. }
  112. private:
  113. /// \brief Check the validation of probabilities vector
  114. ///
  115. /// The probability must be in range of [0, 1.0] and the sum must be equal to 1.0
  116. /// Empty probabilities is also valid.
  117. bool isProbabilitiesValid(const std::vector<double>& probabilities) const
  118. {
  119. typedef std::vector<double>::const_iterator Iterator;
  120. double sum = probabilities.empty() ? 1.0 : 0.0;
  121. for(Iterator it = probabilities.begin(); it != probabilities.end(); ++it){
  122. //The probability must be in [0, 1.0]
  123. if(*it < 0.0 || *it > 1.0) {
  124. return false;
  125. }
  126. sum += *it;
  127. }
  128. double epsilon = 0.0001;
  129. // The sum must be equal to 1
  130. return fabs(sum - 1.0) < epsilon;
  131. }
  132. // Shortcut typedefs
  133. typedef boost::variate_generator<boost::mt19937&, boost::uniform_real<> > UniformRealGenerator;
  134. std::vector<double> cumulative_; ///< The partial sum of the probabilities
  135. boost::mt19937 rng_; ///< Mersenne Twister: A 623-dimensionally equidistributed uniform pseudo-random number generator
  136. boost::uniform_real<> dist_; ///< Uniformly distributed real numbers
  137. UniformRealGenerator uniform_real_gen_; ///< Uniformly distributed random real numbers generator
  138. int min_; ///< The minimum integer that will be generated
  139. };
  140. } // namespace dns
  141. } // namespace isc
  142. #endif//__NSAS_RANDOM_NUMBER_GENERATOR_H