utilities/src/randomgenerator.cpp

00001 /********************************************************************************
00002  *  FARSA Genetic Algorithm Library                                             *
00003  *  Copyright (C) 2007-2008 Gianluca Massera <emmegian@yahoo.it>                *
00004  *                                                                              *
00005  *  This program is free software; you can redistribute it and/or modify        *
00006  *  it under the terms of the GNU General Public License as published by        *
00007  *  the Free Software Foundation; either version 2 of the License, or           *
00008  *  (at your option) any later version.                                         *
00009  *                                                                              *
00010  *  This program is distributed in the hope that it will be useful,             *
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of              *
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
00013  *  GNU General Public License for more details.                                *
00014  *                                                                              *
00015  *  You should have received a copy of the GNU General Public License           *
00016  *  along with this program; if not, write to the Free Software                 *
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA  *
00018  ********************************************************************************/
00019 
00020 #include "randomgenerator.h"
00021 #include <cmath>
00022 
00023 #ifdef FARSA_USE_GSL
00024     #include "gsl/gsl_rng.h"
00025     #include "gsl/gsl_randist.h"
00026 #endif
00027 
00028 namespace farsa {
00029 
00030 class FARSA_UTIL_INTERNAL RandomGeneratorPrivate {
00031 public:
00032 #ifdef FARSA_USE_GSL
00033     gsl_rng* rng;
00034     RandomGeneratorPrivate() {
00035         rng = gsl_rng_alloc( gsl_rng_taus2 );
00036     };
00037     ~RandomGeneratorPrivate() {
00038         gsl_rng_free( rng );
00039     };
00040 #endif
00041 };
00042 
00043 RandomGenerator::RandomGenerator( int seed ) {
00044     prive = new RandomGeneratorPrivate();
00045 #ifdef FARSA_USE_GSL
00046     gsl_rng_set( prive->rng, seed );
00047 #endif
00048 }
00049 
00050 RandomGenerator::~RandomGenerator() {
00051     delete prive;
00052 }
00053 
00054 void RandomGenerator::setSeed( int seed ) {
00055     this->seedv = seed;
00056 #ifdef FARSA_USE_GSL
00057     gsl_rng_set( prive->rng, seed );
00058 #endif
00059 }
00060 
00061 int RandomGenerator::seed() {
00062     return seedv;
00063 }
00064 
00065 bool RandomGenerator::getBool( double trueProbability ) {
00066 #ifdef FARSA_USE_GSL
00067     return ( gsl_rng_uniform( prive->rng ) < trueProbability );
00068 #endif
00069 }
00070 
00071 int RandomGenerator::getInt( int min, int max ) {
00072 #ifdef FARSA_USE_GSL
00073     return ( gsl_rng_uniform_int( prive->rng, qAbs( max-min )+1 ) + min );
00074 #endif
00075 }
00076 
00077 double RandomGenerator::getDouble( double min, double max ) {
00078 #ifdef FARSA_USE_GSL
00079     //--- FIXME this implementation never return max
00080     return gsl_ran_flat( prive->rng, min, max );
00081 #endif
00082 }
00083 
00084 double RandomGenerator::getGaussian( double var, double mean ) {
00085 #ifdef FARSA_USE_GSL
00086     return gsl_ran_gaussian( prive->rng, var ) + mean;
00087 #endif
00088 }
00089 
00090 } // end namespace farsa