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
|
|
|
|
|
2013-06-26 14:18:27 +03:00
|
|
|
typedef std::mt19937 TGenerator;
|
|
|
|
typedef std::uniform_int_distribution<int> TIntDist;
|
|
|
|
typedef std::uniform_real_distribution<double> TRealDist;
|
|
|
|
typedef std::function<int()> TRandI;
|
|
|
|
typedef std::function<double()> TRand;
|
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.
|
|
|
|
class DLL_LINKAGE CRandomGenerator : boost::noncopyable
|
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
|
|
|
|
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
|
|
|
|
2013-04-19 14:43:11 +03:00
|
|
|
/// Generate several integer numbers within the same range.
|
2014-03-17 22:51:07 +03:00
|
|
|
/// e.g.: auto a = gen.getIntRange(0,10); a(); a(); a();
|
|
|
|
/// requires: lower <= upper
|
2014-04-10 20:11:09 +03:00
|
|
|
TRandI getIntRange(int lower, int upper);
|
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
|
2014-04-10 20:11:09 +03:00
|
|
|
int nextInt(int upper);
|
2014-03-17 22:51:07 +03:00
|
|
|
|
|
|
|
/// requires: lower <= upper
|
2014-04-10 20:11:09 +03:00
|
|
|
int nextInt(int lower, int upper);
|
2013-01-06 22:30:12 +03:00
|
|
|
|
2014-03-17 22:51:07 +03:00
|
|
|
/// Generates an integer between 0 and the maximum value it can hold.
|
2014-04-10 20:11:09 +03:00
|
|
|
int nextInt();
|
2014-03-17 22:51:07 +03:00
|
|
|
|
2013-04-19 14:43:11 +03:00
|
|
|
/// Generate several double/real numbers within the same range.
|
2014-03-17 22:51:07 +03:00
|
|
|
/// e.g.: auto a = gen.getDoubleRange(4.5,10.2); a(); a(); a();
|
|
|
|
/// requires: lower <= upper
|
2014-04-10 20:11:09 +03:00
|
|
|
TRand getDoubleRange(double lower, double upper);
|
2013-01-06 22:30:12 +03:00
|
|
|
|
2014-03-17 22:51:07 +03:00
|
|
|
/// Generates a double between 0 and upper.
|
|
|
|
/// requires: 0 <= upper
|
2014-04-10 20:11:09 +03:00
|
|
|
double nextDouble(double upper);
|
2014-03-17 22:51:07 +03:00
|
|
|
|
|
|
|
/// requires: lower <= upper
|
2014-04-10 20:11:09 +03:00
|
|
|
double nextDouble(double lower, double upper);
|
2014-03-17 22:51:07 +03:00
|
|
|
|
|
|
|
/// Generates a double between 0.0 and 1.0.
|
2014-04-10 20:11:09 +03:00
|
|
|
double nextDouble();
|
|
|
|
|
|
|
|
/// 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();
|
|
|
|
|
|
|
|
/// Provide method so that this RNG can be used with legacy std:: API
|
|
|
|
TGenerator & getStdGenerator();
|
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
|
|
|
static boost::thread_specific_ptr<CRandomGenerator> defaultRand;
|
|
|
|
|
|
|
|
public:
|
|
|
|
template <typename Handler>
|
|
|
|
void serialize(Handler & h, const int version)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
|
|
namespace RandomGeneratorUtil
|
|
|
|
{
|
|
|
|
template<typename Container>
|
|
|
|
auto nextItem(const Container & container, CRandomGenerator & rand) -> decltype(std::begin(container))
|
|
|
|
{
|
|
|
|
assert(!container.empty());
|
|
|
|
return std::next(container.begin(), rand.nextInt(container.size() - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Container>
|
|
|
|
auto nextItem(Container & container, CRandomGenerator & rand) -> decltype(std::begin(container))
|
|
|
|
{
|
|
|
|
assert(!container.empty());
|
|
|
|
return std::next(container.begin(), rand.nextInt(container.size() - 1));
|
|
|
|
}
|
2014-06-07 15:02:57 +03:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void randomShuffle(std::vector<T>& container, CRandomGenerator & rand)
|
|
|
|
{
|
|
|
|
int n = (container.end() - container.begin());
|
|
|
|
for (int i = n-1; i>0; --i)
|
|
|
|
{
|
|
|
|
std::swap (container.begin()[i],container.begin()[rand.nextInt(i+1)]);
|
|
|
|
}
|
|
|
|
}
|
2014-03-17 22:51:07 +03:00
|
|
|
}
|