Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

Random.h

Go to the documentation of this file.
00001 /*
00002     liban - base function to do usefull things related to simulation and scientific work
00003     Copyright (C) 1999-2005 Stephane Magnenat <stephane at magnenat dot net>
00004     Copyright (c) 2004-2005 Antoine Beyeler <antoine dot beyeler at epfl dot ch>
00005     Copyright (C) 2005 Laboratory of Intelligent Systems, EPFL, Lausanne
00006     See AUTHORS for details
00007 
00008     This program is free software. You can redistribute it and/or modify
00009     it under the terms of the GNU General Public License as published by
00010     the Free Software Foundation; either version 2 of the License, or
00011     (at your option) any later version.
00012 
00013     This program is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU General Public License for more details.
00017 
00018     You should have received a copy of the GNU General Public License
00019     along with this program; if not, write to the Free Software
00020     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 */
00022 
00023 #ifndef __AN_RANDOM_H
00024 #define __AN_RANDOM_H
00025 
00026 #include <cmath>
00027 #include <cstdlib>
00028 
00033 namespace An
00034 {
00036 
00037     class FastRandom
00038     {
00039     private:
00040         unsigned long randx; 
00041     
00042     public:
00044         FastRandom(void) { randx = 0; }
00046         void setSeed(unsigned long seed) { randx = seed; }
00048         unsigned long get(void) { return (randx = randx*1103515245 + 12345) & 0x7fffffff; }
00050         double getRange(double range) { return (static_cast<double>(get()) * range) / 2147483648.0; }
00051     };
00052     
00054 
00055     inline double uniformRand(void)
00056     {
00057         return double(rand())/RAND_MAX;
00058     }
00059     
00061     struct UniformRand
00062     {
00063         double from; 
00064         double to;
00065         
00067         UniformRand(double from = 0.0, double to = 1.0) { this->from = from; this->to = to; }
00068         
00070         double operator()() const { return from + (to-from)*uniformRand(); }
00071     };
00072     
00074 
00075     inline unsigned intRand(unsigned max)
00076     {
00077         if (max)
00078             return rand() % max;
00079         else
00080             return 0;
00081     }
00082     
00084 
00085     inline bool boolRand(double prob = 0.5)
00086     {
00087         return uniformRand() < prob;
00088     }
00089     
00091 
00092     inline double gaussianRand(double mean, double sigm)
00093     {
00094         // Generation using the Polar (Box-Mueller) method.
00095         // Code inspired by GSL, which is a really great math lib.
00096         // http://sources.redhat.com/gsl/
00097         // C++ wrapper available.
00098         // http://gslwrap.sourceforge.net/
00099         double r, x, y;
00100         
00101         // Generate random number in unity circle.
00102         do
00103         {
00104             x = uniformRand()*2 - 1;
00105             y = uniformRand()*2 - 1;
00106             r = x*x + y*y;
00107         }
00108         while (r > 1.0 || r == 0);
00109         
00110         // Box-Muller transform.
00111         return sigm * y * sqrt (-2.0 * log(r) / r) + mean;
00112     }
00113 }
00114 
00115 #endif

Generated on Mon Oct 24 17:30:33 2005 for liban by  doxygen 1.4.2