CRandomGenerator.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. typedef std::mt19937 TGenerator;
  12. typedef std::uniform_int_distribution<int> TIntDist;
  13. typedef std::uniform_real_distribution<double> TRealDist;
  14. typedef std::function<int()> TRandI;
  15. typedef std::function<double()> TRand;
  16. /// The random generator randomly generates integers and real numbers("doubles") between
  17. /// a given range. This is a header only class and mainly a wrapper for
  18. /// convenient usage of the standard random API. An instance of this RNG is not thread safe.
  19. class DLL_LINKAGE CRandomGenerator : boost::noncopyable
  20. {
  21. public:
  22. /// Seeds the generator by default with the product of the current time in milliseconds and the
  23. /// current thread ID.
  24. CRandomGenerator();
  25. void setSeed(int seed);
  26. /// Resets the seed to the product of the current time in milliseconds and the
  27. /// current thread ID.
  28. void resetSeed();
  29. /// Generate several integer numbers within the same range.
  30. /// e.g.: auto a = gen.getIntRange(0,10); a(); a(); a();
  31. /// requires: lower <= upper
  32. TRandI getIntRange(int lower, int upper);
  33. /// Generates an integer between 0 and upper.
  34. /// requires: 0 <= upper
  35. int nextInt(int upper);
  36. /// requires: lower <= upper
  37. int nextInt(int lower, int upper);
  38. /// Generates an integer between 0 and the maximum value it can hold.
  39. int nextInt();
  40. /// Generate several double/real numbers within the same range.
  41. /// e.g.: auto a = gen.getDoubleRange(4.5,10.2); a(); a(); a();
  42. /// requires: lower <= upper
  43. TRand getDoubleRange(double lower, double upper);
  44. /// Generates a double between 0 and upper.
  45. /// requires: 0 <= upper
  46. double nextDouble(double upper);
  47. /// requires: lower <= upper
  48. double nextDouble(double lower, double upper);
  49. /// Generates a double between 0.0 and 1.0.
  50. double nextDouble();
  51. /// Gets a globally accessible RNG which will be constructed once per thread. For the
  52. /// seed a combination of the thread ID and current time in milliseconds will be used.
  53. static CRandomGenerator & getDefault();
  54. /// Provide method so that this RNG can be used with legacy std:: API
  55. TGenerator & getStdGenerator();
  56. private:
  57. TGenerator rand;
  58. static boost::thread_specific_ptr<CRandomGenerator> defaultRand;
  59. public:
  60. template <typename Handler>
  61. void serialize(Handler & h, const int version)
  62. {
  63. if(h.saving)
  64. {
  65. std::ostringstream stream;
  66. stream << rand;
  67. std::string str = stream.str();
  68. h & str;
  69. }
  70. else
  71. {
  72. std::string str;
  73. h & str;
  74. std::istringstream stream(str);
  75. stream >> rand;
  76. }
  77. }
  78. };
  79. namespace RandomGeneratorUtil
  80. {
  81. /// Gets an iterator to an element of a nonempty container randomly. Undefined behaviour if container is empty.
  82. //template<typename T>
  83. //auto nextItem(const std::set<T> & container, CRandomGenerator & rand) -> decltype(std::begin(container))
  84. //{
  85. // assert(!container.empty());
  86. // auto ret = container.begin();
  87. // std::advance(ret, rand.nextInt(container.size() - 1));
  88. // return ret;
  89. //}
  90. //template<typename T>
  91. //auto nextItem(std::set<T> & container, CRandomGenerator & rand) -> decltype(std::begin(container))
  92. //{
  93. // assert(!container.empty());
  94. // auto ret = container.begin();
  95. // std::advance(ret, rand.nextInt(container.size() - 1));
  96. // return ret;
  97. //}
  98. template<typename Container>
  99. auto nextItem(const Container & container, CRandomGenerator & rand) -> decltype(std::begin(container))
  100. {
  101. assert(!container.empty());
  102. return std::next(container.begin(), rand.nextInt(container.size() - 1));
  103. }
  104. template<typename Container>
  105. auto nextItem(Container & container, CRandomGenerator & rand) -> decltype(std::begin(container))
  106. {
  107. assert(!container.empty());
  108. return std::next(container.begin(), rand.nextInt(container.size() - 1));
  109. }
  110. }