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