CRandomGenerator.h 2.8 KB

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