Differential Evolution C++ library
C:/dev/de/differentialevolution/random_generator.hpp
00001 /*
00002  * Copyright (c) 2011 Adrian Michel
00003  * http://www.amichel.com
00004  *
00005  * Permission to use, copy, modify, distribute and sell this 
00006  * software and its documentation for any purpose is hereby 
00007  * granted without fee, provided that both the above copyright 
00008  * notice and this permission notice appear in all copies and in 
00009  * the supporting documentation. 
00010  *  
00011  * This library is distributed in the hope that it will be 
00012  * useful. However, Adrian Michel makes no representations about
00013  * the suitability of this software for any purpose.  It is 
00014  * provided "as is" without any express or implied warranty. 
00015  * 
00016  * Should you find this library useful, please email 
00017  * info@amichel.com with a link or other reference 
00018  * to your work. 
00019 */
00020 
00021 #ifndef DE_RANDOM_GENERATOR_HPP_INCLUDED
00022 #define DE_RANDOM_GENERATOR_HPP_INCLUDED
00023 
00024 // MS compatible compilers support #pragma once
00025 
00026 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
00027 #pragma once
00028 #endif
00029 
00030 #include <boost/math/special_functions/round.hpp>
00031 #include <boost/random/uniform_real_distribution.hpp>
00032 #include <boost/random/uniform_int.hpp>
00033 #include <boost/random/variate_generator.hpp>
00034 #include <boost/random/mersenne_twister.hpp>
00035 
00036 namespace de
00037 {
00038 
00039 inline double genrand( double min = 0, double max = 1 )
00040 {
00041         static boost::random::mt19937 gen;
00042         boost::random::uniform_real_distribution<> dist( min, max );
00043         boost::variate_generator<boost::random::mt19937&, boost::random::uniform_real_distribution< double > > value(gen, dist);
00044 
00045         return value();
00046 }
00047 
00048 inline int genintrand( double min, double max, bool upperexclusive = false )
00049 {
00050         assert( min < max );
00051         int ret = 0;
00052         do ret = boost::math::round( genrand( min, max ) ); while( ret < min || ret > max || upperexclusive && ret == max );
00053         return ret; 
00054 }
00055 
00056 }
00057 
00058 #endif //DE_RANDOM_GENERATOR_HPP_INCLUDED