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
|
2013-08-17 15:46:48 +03:00
|
|
|
/// convenient usage of the standard random API.
|
2013-01-06 22:30:12 +03:00
|
|
|
class CRandomGenerator
|
|
|
|
{
|
|
|
|
public:
|
2013-04-19 14:43:11 +03:00
|
|
|
/// Seeds the generator with the current time by default.
|
2013-01-06 22:30:12 +03:00
|
|
|
CRandomGenerator()
|
|
|
|
{
|
2013-04-19 14:43:11 +03:00
|
|
|
gen.seed(std::time(nullptr));
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void seed(int value)
|
|
|
|
{
|
|
|
|
gen.seed(value);
|
|
|
|
}
|
|
|
|
|
2013-04-19 14:43:11 +03:00
|
|
|
/// Generate several integer numbers within the same range.
|
|
|
|
/// e.g.: auto a = gen.getRangeI(0,10); a(); a(); a();
|
2013-01-06 22:30:12 +03:00
|
|
|
TRandI getRangeI(int lower, int upper)
|
|
|
|
{
|
2013-08-17 15:46:48 +03:00
|
|
|
return boost::bind(TIntDist(lower, upper), boost::ref(gen));
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int getInteger(int lower, int upper)
|
|
|
|
{
|
|
|
|
return getRangeI(lower, upper)();
|
|
|
|
}
|
|
|
|
|
2013-04-19 14:43:11 +03:00
|
|
|
/// Generate several double/real numbers within the same range.
|
|
|
|
/// e.g.: auto a = gen.getRangeI(0,10); a(); a(); a();
|
2013-01-06 22:30:12 +03:00
|
|
|
TRand getRange(double lower, double upper)
|
|
|
|
{
|
2013-08-17 15:46:48 +03:00
|
|
|
return boost::bind(TRealDist(lower, upper), boost::ref(gen));
|
2013-01-06 22:30:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
double getDouble(double lower, double upper)
|
|
|
|
{
|
|
|
|
return getRange(lower, upper)();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
TGenerator gen;
|
|
|
|
};
|