enki/Random.h

Go to the documentation of this file.
00001 /*
00002     Enki - a fast 2D robot simulator
00003     Copyright (C) 1999-2008 Stephane Magnenat <stephane at magnenat dot net>
00004     Copyright (C) 2004-2005 Markus Waibel <markus dot waibel at epfl dot ch>
00005     Copyright (c) 2004-2005 Antoine Beyeler <abeyeler at ab-ware dot com>
00006     Copyright (C) 2005-2006 Laboratory of Intelligent Systems, EPFL, Lausanne
00007     Copyright (C) 2006-2008 Laboratory of Robotics Systems, EPFL, Lausanne
00008     See AUTHORS for details
00009 
00010     This program is free software; the authors of any publication 
00011     arising from research using this software are asked to add the 
00012     following reference:
00013     Enki - a fast 2D robot simulator
00014     http://lis.epfl.ch/enki
00015     Stephane Magnenat <stephane at magnenat dot net>,
00016     Markus Waibel <markus dot waibel at epfl dot ch>
00017     Laboratory of Intelligent Systems, EPFL, Lausanne.
00018 
00019     You can redistribute this program and/or modify
00020     it under the terms of the GNU General Public License as published by
00021     the Free Software Foundation; either version 2 of the License, or
00022     (at your option) any later version.
00023 
00024     This program is distributed in the hope that it will be useful,
00025     but WITHOUT ANY WARRANTY; without even the implied warranty of
00026     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00027     GNU General Public License for more details.
00028 
00029     You should have received a copy of the GNU General Public License
00030     along with this program; if not, write to the Free Software
00031     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00032 */
00033 
00034 #ifndef __ENKI_RANDOM_H
00035 #define __ENKI_RANDOM_H
00036 
00037 #include <cmath>
00038 #include <cstdlib>
00039 
00044 namespace Enki
00045 {
00047 
00048     class FastRandom
00049     {
00050     private:
00051         unsigned long randx; 
00052     
00053     public:
00055         FastRandom(void) { randx = 0; }
00057         void setSeed(unsigned long seed) { randx = seed; }
00059         unsigned long get(void) { return (randx = randx*1103515245 + 12345) & 0x7fffffff; }
00061         double getRange(double range) { return (static_cast<double>(get()) * range) / 2147483648.0; }
00062     };
00063     
00065 
00066     inline double uniformRand(void)
00067     {
00068         return double(rand())/RAND_MAX;
00069     }
00070     
00072     struct UniformRand
00073     {
00074         double from; 
00075         double to;
00076         
00078         UniformRand(double from = 0.0, double to = 1.0) { this->from = from; this->to = to; }
00079         
00081         double operator()() const { return from + (to-from)*uniformRand(); }
00082     };
00083     
00085 
00086     inline unsigned intRand(unsigned max)
00087     {
00088         if (max)
00089             return rand() % max;
00090         else
00091             return 0;
00092     }
00093     
00095 
00096     inline bool boolRand(double prob = 0.5)
00097     {
00098         return uniformRand() < prob;
00099     }
00100     
00102 
00103     inline double gaussianRand(double mean, double sigm)
00104     {
00105         // Generation using the Polar (Box-Mueller) method.
00106         // Code inspired by GSL, which is a really great math lib.
00107         // http://sources.redhat.com/gsl/
00108         // C++ wrapper available.
00109         // http://gslwrap.sourceforge.net/
00110         double r, x, y;
00111         
00112         // Generate random number in unity circle.
00113         do
00114         {
00115             x = uniformRand()*2 - 1;
00116             y = uniformRand()*2 - 1;
00117             r = x*x + y*y;
00118         }
00119         while (r > 1.0 || r == 0);
00120         
00121         // Box-Muller transform.
00122         return sigm * y * sqrt (-2.0 * log(r) / r) + mean;
00123     }
00124 }
00125 
00126 #endif

Generated on Sun Mar 1 03:10:09 2009 for Enki by  doxygen 1.5.1