CRandomGenerator.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * CRandomGenerator.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include <boost/version.hpp>
  12. #include <boost/random/mersenne_twister.hpp>
  13. #if BOOST_VERSION >= 104700
  14. #include <boost/random/uniform_int_distribution.hpp>
  15. #include <boost/random/uniform_real_distribution.hpp>
  16. #else
  17. #include <boost/random/uniform_int.hpp>
  18. #include <boost/random/uniform_real.hpp>
  19. #endif
  20. #include <boost/random/variate_generator.hpp>
  21. typedef boost::mt19937 TGenerator;
  22. #if BOOST_VERSION >= 104700
  23. typedef boost::random::uniform_int_distribution<int> TIntDist;
  24. typedef boost::random::uniform_real_distribution<double> TRealDist;
  25. #else
  26. typedef boost::uniform_int<int> TIntDist;
  27. typedef boost::uniform_real<double> TRealDist;
  28. #endif
  29. typedef boost::variate_generator<TGenerator &, TIntDist> TRandI;
  30. typedef boost::variate_generator<TGenerator &, TRealDist> TRand;
  31. /**
  32. * The random generator randomly generates integers and real numbers("doubles") between
  33. * a given range. This is a header only class and mainly a wrapper for
  34. * convenient usage of the boost random API.
  35. */
  36. class CRandomGenerator
  37. {
  38. public:
  39. /**
  40. * Constructor. Seeds the generator with the current time by default.
  41. */
  42. CRandomGenerator()
  43. {
  44. gen.seed(std::time(nullptr));
  45. }
  46. /**
  47. * Seeds the generator with the given value.
  48. *
  49. * @param value the random seed
  50. */
  51. void seed(int value)
  52. {
  53. gen.seed(value);
  54. }
  55. /**
  56. * Gets a generator which generates integers in the given range.
  57. *
  58. * Example how to use:
  59. * @code
  60. * TRandI rand = getRangeI(0, 10);
  61. * int a = rand(); // with the operator() the next value can be obtained
  62. * int b = rand(); // you can generate more values
  63. * @endcode
  64. *
  65. * @param lower the lower boundary
  66. * @param upper the upper boundary
  67. * @return the generator which can be used to generate integer numbers
  68. */
  69. TRandI getRangeI(int lower, int upper)
  70. {
  71. TIntDist range(lower, upper);
  72. return TRandI(gen, range);
  73. }
  74. /**
  75. * Gets a integer in the given range. In comparison to getRangeI it's
  76. * a convenient method if you want to generate only one value in a given
  77. * range.
  78. *
  79. * @param lower the lower boundary
  80. * @param upper the upper boundary
  81. * @return the generated integer
  82. */
  83. int getInteger(int lower, int upper)
  84. {
  85. return getRangeI(lower, upper)();
  86. }
  87. /**
  88. * Gets a generator which generates doubles in the given range.
  89. *
  90. * Example how to use:
  91. * @code
  92. * TRand rand = getRange(23.56, 32.10);
  93. * double a = rand(); // with the operator() the next value can be obtained
  94. * double b = rand(); // you can generate more values
  95. * @endcode
  96. *
  97. * @param lower the lower boundary
  98. * @param upper the upper boundary
  99. * @return the generated double
  100. */
  101. TRand getRange(double lower, double upper)
  102. {
  103. TRealDist range(lower, upper);
  104. return TRand(gen, range);
  105. }
  106. /**
  107. * Gets a double in the given range. In comparison to getRange it's
  108. * a convenient method if you want to generate only one value in a given
  109. * range.
  110. *
  111. * @param lower the lower boundary
  112. * @param upper the upper boundary
  113. * @return the generated double
  114. */
  115. double getDouble(double lower, double upper)
  116. {
  117. return getRange(lower, upper)();
  118. }
  119. private:
  120. /** The actual boost random generator. */
  121. TGenerator gen;
  122. };