00001 #include "UniformRNG.h" 00002 #include <ctime> 00003 #include <boost/cast.hpp> 00004 00005 using namespace std; 00006 00007 namespace gplib 00008 { 00009 //the constructor sets up the boost random number generator 00010 UniformRNG::UniformRNG() : 00011 generator(static_cast<unsigned int> (std::time(NULL))), real_dist( 00012 generator) 00013 { 00014 00015 } 00016 00017 UniformRNG::~UniformRNG() 00018 { 00019 } 00020 00021 float UniformRNG::GetNumber(const float low, const float high) 00022 { 00023 //return float between low and high 00024 return low + (high - low) * GetNumber(); 00025 } 00026 00027 float UniformRNG::GetNumber() 00028 { 00029 //just forward the call to the boost random number generator 00030 return real_dist(); 00031 } 00032 00033 unsigned int UniformRNG::GetNumber(const unsigned int max) 00034 { 00035 //make an unsigned integer from the random float 00036 return (boost::numeric_cast<unsigned int>(max * real_dist())); 00037 } 00038 }
1.5.8