CRandomGenerator.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.
  19. class CRandomGenerator : boost::noncopyable
  20. {
  21. public:
  22. /// Seeds the generator with the current time by default.
  23. CRandomGenerator()
  24. {
  25. rand.seed(static_cast<unsigned long>(std::time(nullptr)));
  26. }
  27. void setSeed(int seed)
  28. {
  29. rand.seed(seed);
  30. }
  31. /// Generate several integer numbers within the same range.
  32. /// e.g.: auto a = gen.getIntRange(0,10); a(); a(); a();
  33. /// requires: lower <= upper
  34. TRandI getIntRange(int lower, int upper)
  35. {
  36. return boost::bind(TIntDist(lower, upper), boost::ref(rand));
  37. }
  38. /// Generates an integer between 0 and upper.
  39. /// requires: 0 <= upper
  40. int nextInt(int upper)
  41. {
  42. return getIntRange(0, upper)();
  43. }
  44. /// requires: lower <= upper
  45. int nextInt(int lower, int upper)
  46. {
  47. return getIntRange(lower, upper)();
  48. }
  49. /// Generates an integer between 0 and the maximum value it can hold.
  50. int nextInt()
  51. {
  52. return TIntDist()(rand);
  53. }
  54. /// Generate several double/real numbers within the same range.
  55. /// e.g.: auto a = gen.getDoubleRange(4.5,10.2); a(); a(); a();
  56. /// requires: lower <= upper
  57. TRand getDoubleRange(double lower, double upper)
  58. {
  59. return boost::bind(TRealDist(lower, upper), boost::ref(rand));
  60. }
  61. /// Generates a double between 0 and upper.
  62. /// requires: 0 <= upper
  63. double nextDouble(double upper)
  64. {
  65. return getDoubleRange(0, upper)();
  66. }
  67. /// requires: lower <= upper
  68. double nextDouble(double lower, double upper)
  69. {
  70. return getDoubleRange(lower, upper)();
  71. }
  72. /// Generates a double between 0.0 and 1.0.
  73. double nextDouble()
  74. {
  75. return TRealDist()(rand);
  76. }
  77. private:
  78. TGenerator rand;
  79. };
  80. namespace RandomGeneratorUtil
  81. {
  82. /// Gets an iterator to an element of a nonempty container randomly. Undefined behaviour if container is empty.
  83. template<typename Container>
  84. auto nextItem(const Container & container, CRandomGenerator & rand) -> decltype(std::begin(container))
  85. {
  86. assert(!container.empty());
  87. return std::next(container.begin(), rand.nextInt(container.size() - 1));
  88. }
  89. template<typename Container>
  90. auto nextItem(Container & container, CRandomGenerator & rand) -> decltype(std::begin(container))
  91. {
  92. assert(!container.empty());
  93. return std::next(container.begin(), rand.nextInt(container.size() - 1));
  94. }
  95. }