2013-01-06 22:30:12 +03:00
|
|
|
/*
|
|
|
|
* CRandomGenerator.h, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
#include <vstd/RNG.h>
|
2024-05-07 21:17:05 +02:00
|
|
|
#include "serializer/Serializeable.h"
|
2017-07-20 06:08:49 +02:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2024-06-01 15:36:38 +02:00
|
|
|
|
2023-11-07 23:48:12 +02:00
|
|
|
/// Generator to use for all randomization in game
|
|
|
|
/// minstd_rand is selected due to following reasons:
|
|
|
|
/// 1. Its randomization quality is below mt_19937 however this is unlikely to be noticeable in game
|
|
|
|
/// 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)
|
2023-11-06 23:35:28 +02:00
|
|
|
using TGenerator = std::minstd_rand;
|
2023-04-17 23:11:16 +02:00
|
|
|
using TIntDist = std::uniform_int_distribution<int>;
|
|
|
|
using TInt64Dist = std::uniform_int_distribution<int64_t>;
|
|
|
|
using TRealDist = std::uniform_real_distribution<double>;
|
2013-01-06 22:30:12 +03:00
|
|
|
|
2013-04-19 14:43:11 +03:00
|
|
|
/// The random generator randomly generates integers and real numbers("doubles") between
|
|
|
|
/// a given range. This is a header only class and mainly a wrapper for
|
2014-04-10 20:11:09 +03:00
|
|
|
/// convenient usage of the standard random API. An instance of this RNG is not thread safe.
|
2024-06-01 15:36:38 +02:00
|
|
|
class DLL_LINKAGE CRandomGenerator final : public vstd::RNG, boost::noncopyable, public Serializeable
|
2013-01-06 22:30:12 +03:00
|
|
|
{
|
|
|
|
public:
|
2014-04-10 20:11:09 +03:00
|
|
|
/// Seeds the generator by default with the product of the current time in milliseconds and the
|
|
|
|
/// current thread ID.
|
|
|
|
CRandomGenerator();
|
2013-01-06 22:30:12 +03:00
|
|
|
|
2023-07-11 18:23:14 +02:00
|
|
|
/// Seeds the generator with provided initial seed
|
|
|
|
explicit CRandomGenerator(int seed);
|
|
|
|
|
2014-04-10 20:11:09 +03:00
|
|
|
void setSeed(int seed);
|
|
|
|
|
|
|
|
/// Resets the seed to the product of the current time in milliseconds and the
|
|
|
|
/// current thread ID.
|
|
|
|
void resetSeed();
|
2013-01-06 22:30:12 +03:00
|
|
|
|
2014-03-17 22:51:07 +03:00
|
|
|
/// Generates an integer between 0 and upper.
|
|
|
|
/// requires: 0 <= upper
|
2024-06-01 17:28:17 +02:00
|
|
|
int nextInt(int upper) override;
|
|
|
|
int64_t nextInt64(int64_t upper) override;
|
2014-03-17 22:51:07 +03:00
|
|
|
|
|
|
|
/// requires: lower <= upper
|
2024-06-01 15:36:38 +02:00
|
|
|
int nextInt(int lower, int upper) override;
|
|
|
|
int64_t nextInt64(int64_t lower, int64_t upper) override;
|
2017-07-20 06:08:49 +02:00
|
|
|
|
2014-03-17 22:51:07 +03:00
|
|
|
/// Generates an integer between 0 and the maximum value it can hold.
|
2024-06-01 17:28:17 +02:00
|
|
|
int nextInt() override;
|
|
|
|
|
|
|
|
///
|
|
|
|
int nextBinomialInt(int coinsCount, double coinChance) override;
|
|
|
|
|
2014-03-17 22:51:07 +03:00
|
|
|
|
|
|
|
/// Generates a double between 0 and upper.
|
|
|
|
/// requires: 0 <= upper
|
2024-06-01 17:28:17 +02:00
|
|
|
double nextDouble(double upper) override;
|
2014-03-17 22:51:07 +03:00
|
|
|
|
|
|
|
/// requires: lower <= upper
|
2024-06-01 15:36:38 +02:00
|
|
|
double nextDouble(double lower, double upper) override;
|
2014-04-10 20:11:09 +03:00
|
|
|
|
|
|
|
/// Gets a globally accessible RNG which will be constructed once per thread. For the
|
|
|
|
/// seed a combination of the thread ID and current time in milliseconds will be used.
|
|
|
|
static CRandomGenerator & getDefault();
|
|
|
|
|
2013-01-06 22:30:12 +03:00
|
|
|
private:
|
2014-03-17 22:51:07 +03:00
|
|
|
TGenerator rand;
|
2014-04-10 20:11:09 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
template <typename Handler>
|
2024-01-20 20:34:51 +02:00
|
|
|
void serialize(Handler & h)
|
2014-04-10 20:11:09 +03:00
|
|
|
{
|
|
|
|
if(h.saving)
|
|
|
|
{
|
|
|
|
std::ostringstream stream;
|
|
|
|
stream << rand;
|
|
|
|
std::string str = stream.str();
|
|
|
|
h & str;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string str;
|
|
|
|
h & str;
|
|
|
|
std::istringstream stream(str);
|
|
|
|
stream >> rand;
|
|
|
|
}
|
|
|
|
}
|
2013-01-06 22:30:12 +03:00
|
|
|
};
|
2014-03-17 22:51:07 +03:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|