for my program I need pseudo random integers with different ranges. So far I have used the rand () function, but it has limitations.
I found that boost :: random library is a much better replacement, but I did not want to create random generators everywhere.
(I need random integers in many classes because it is stress test software that makes every solution pseudo-random (-> the trial run must be repeated, setting the same seed)).
This is why I missed boost :: random in my class.
The idea behind this is to make it easier to use, so that it is almost as simple as the C ++ rand () method
#include "boost/shared_ptr.hpp"
#include "boost/random.hpp"
class Random{
public:
typedef boost::shared_ptr< Random > randomPtr;
typedef boost::mt19937 randomGeneratorType;
static randomPtr Get(){
static randomPtr randomGen( new RandomGenerator() );
return randomGen;
}
void SetSeed(int seed){
randomGenerator.seed( seed );
}
int Random( int lowerLimit, int upperLimit ){
boost::uniform_int<> distribution( lowerLimit, upperLimit );
boost::variate_generator< randomGeneratorType&, boost::uniform_int<> >
LimitedInt( randomGenerator , distribution );
return LimitedInt();
}
private:
Random():
randomGenerator()
{}
RandomGenerator( const RandomGenerator& orig ){};
randomGeneratorType randomGenerator;
};
,
#include "Random.h"
Random::Get()->SetSeed( 123123 );
int dice = Random::Get()->Random(1,6);
:
- ?
?
Pure Evil ?
( ++ , ,
)