CRandomGenerator.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 <vstd/RNG.h>
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. using TGenerator = std::mt19937;
  14. using TIntDist = std::uniform_int_distribution<int>;
  15. using TInt64Dist = std::uniform_int_distribution<int64_t>;
  16. using TRealDist = std::uniform_real_distribution<double>;
  17. using TRandI = std::function<int()>;
  18. /// The random generator randomly generates integers and real numbers("doubles") between
  19. /// a given range. This is a header only class and mainly a wrapper for
  20. /// convenient usage of the standard random API. An instance of this RNG is not thread safe.
  21. class DLL_LINKAGE CRandomGenerator : public vstd::RNG, boost::noncopyable
  22. {
  23. public:
  24. /// Seeds the generator by default with the product of the current time in milliseconds and the
  25. /// current thread ID.
  26. CRandomGenerator();
  27. /// Seeds the generator with provided initial seed
  28. explicit CRandomGenerator(int seed);
  29. void setSeed(int seed);
  30. /// Resets the seed to the product of the current time in milliseconds and the
  31. /// current thread ID.
  32. void resetSeed();
  33. /// Generate several integer numbers within the same range.
  34. /// e.g.: auto a = gen.getIntRange(0,10); a(); a(); a();
  35. /// requires: lower <= upper
  36. TRandI getIntRange(int lower, int upper);
  37. vstd::TRandI64 getInt64Range(int64_t lower, int64_t upper) override;
  38. /// Generates an integer between 0 and upper.
  39. /// requires: 0 <= upper
  40. int nextInt(int upper);
  41. /// requires: lower <= upper
  42. int nextInt(int lower, int upper);
  43. /// Generates an integer between 0 and the maximum value it can hold.
  44. int nextInt();
  45. /// Generate several double/real numbers within the same range.
  46. /// e.g.: auto a = gen.getDoubleRange(4.5,10.2); a(); a(); a();
  47. /// requires: lower <= upper
  48. vstd::TRand getDoubleRange(double lower, double upper) override;
  49. /// Generates a double between 0 and upper.
  50. /// requires: 0 <= upper
  51. double nextDouble(double upper);
  52. /// requires: lower <= upper
  53. double nextDouble(double lower, double upper);
  54. /// Generates a double between 0.0 and 1.0.
  55. double nextDouble();
  56. /// Gets a globally accessible RNG which will be constructed once per thread. For the
  57. /// seed a combination of the thread ID and current time in milliseconds will be used.
  58. static CRandomGenerator & getDefault();
  59. /// Provide method so that this RNG can be used with legacy std:: API
  60. TGenerator & getStdGenerator();
  61. private:
  62. TGenerator rand;
  63. public:
  64. template <typename Handler>
  65. void serialize(Handler & h, const int version)
  66. {
  67. if(h.saving)
  68. {
  69. std::ostringstream stream;
  70. stream << rand;
  71. std::string str = stream.str();
  72. h & str;
  73. }
  74. else
  75. {
  76. std::string str;
  77. h & str;
  78. std::istringstream stream(str);
  79. stream >> rand;
  80. }
  81. }
  82. };
  83. VCMI_LIB_NAMESPACE_END