CRandomGenerator.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/random/mersenne_twister.hpp>
  12. #include <boost/random/uniform_int_distribution.hpp>
  13. #include <boost/random/uniform_real_distribution.hpp>
  14. #include <boost/random/variate_generator.hpp>
  15. typedef boost::mt19937 TGenerator;
  16. typedef boost::random::uniform_int_distribution<int> TIntDist;
  17. typedef boost::random::uniform_real_distribution<double> TRealDist;
  18. typedef boost::variate_generator<TGenerator &, TIntDist> TRandI;
  19. typedef boost::variate_generator<TGenerator &, TRealDist> TRand;
  20. /**
  21. * The random generator randomly generates integers and real numbers("doubles") between
  22. * a given range. This is a header only class and mainly a wrapper for
  23. * convenient usage of the boost random API.
  24. */
  25. class CRandomGenerator
  26. {
  27. public:
  28. /**
  29. * Constructor. Seeds the generator with the current time by default.
  30. */
  31. CRandomGenerator()
  32. {
  33. gen.seed(std::time(nullptr));
  34. }
  35. /**
  36. * Seeds the generator with the given value.
  37. *
  38. * @param value the random seed
  39. */
  40. void seed(int value)
  41. {
  42. gen.seed(value);
  43. }
  44. /**
  45. * Gets a generator which generates integers in the given range.
  46. *
  47. * Example how to use:
  48. * @code
  49. * TRandI rand = getRangeI(0, 10);
  50. * int a = rand(); // with the operator() the next value can be obtained
  51. * int b = rand(); // you can generate more values
  52. * @endcode
  53. *
  54. * @param lower the lower boundary
  55. * @param upper the upper boundary
  56. * @return the generator which can be used to generate integer numbers
  57. */
  58. TRandI getRangeI(int lower, int upper)
  59. {
  60. TIntDist range(lower, upper);
  61. return TRandI(gen, range);
  62. }
  63. /**
  64. * Gets a integer in the given range. In comparison to getRangeI it's
  65. * a convenient method if you want to generate only one value in a given
  66. * range.
  67. *
  68. * @param lower the lower boundary
  69. * @param upper the upper boundary
  70. * @return the generated integer
  71. */
  72. int getInteger(int lower, int upper)
  73. {
  74. return getRangeI(lower, upper)();
  75. }
  76. /**
  77. * Gets a generator which generates doubles in the given range.
  78. *
  79. * Example how to use:
  80. * @code
  81. * TRand rand = getRange(23.56, 32.10);
  82. * double a = rand(); // with the operator() the next value can be obtained
  83. * double b = rand(); // you can generate more values
  84. * @endcode
  85. *
  86. * @param lower the lower boundary
  87. * @param upper the upper boundary
  88. * @return the generated double
  89. */
  90. TRand getRange(double lower, double upper)
  91. {
  92. TRealDist range(lower, upper);
  93. return TRand(gen, range);
  94. }
  95. /**
  96. * Gets a double in the given range. In comparison to getRange it's
  97. * a convenient method if you want to generate only one value in a given
  98. * range.
  99. *
  100. * @param lower the lower boundary
  101. * @param upper the upper boundary
  102. * @return the generated double
  103. */
  104. double getDouble(double lower, double upper)
  105. {
  106. return getRange(lower, upper)();
  107. }
  108. private:
  109. /** The actual boost random generator. */
  110. TGenerator gen;
  111. };