Image Component Library (ICL)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Random.h
Go to the documentation of this file.
00001 /********************************************************************
00002 **                Image Component Library (ICL)                    **
00003 **                                                                 **
00004 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld          **
00005 **                         Neuroinformatics Group                  **
00006 ** Website: www.iclcv.org and                                      **
00007 **          http://opensource.cit-ec.de/projects/icl               **
00008 **                                                                 **
00009 ** File   : ICLUtils/src/ICLUtils/Random.h                         **
00010 ** Module : ICLUtils                                               **
00011 ** Authors: Christof Elbrechter                                    **
00012 **                                                                 **
00013 **                                                                 **
00014 ** GNU LESSER GENERAL PUBLIC LICENSE                               **
00015 ** This file may be used under the terms of the GNU Lesser General **
00016 ** Public License version 3.0 as published by the                  **
00017 **                                                                 **
00018 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
00019 ** included in the packaging of this file.  Please review the      **
00020 ** following information to ensure the license requirements will   **
00021 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt                **
00022 **                                                                 **
00023 ** The development of this software was supported by the           **
00024 ** Excellence Cluster EXC 277 Cognitive Interaction Technology.    **
00025 ** The Excellence Cluster EXC 277 is a grant of the Deutsche       **
00026 ** Forschungsgemeinschaft (DFG) in the context of the German       **
00027 ** Excellence Initiative.                                          **
00028 **                                                                 **
00029 ********************************************************************/
00030 
00031 #pragma once
00032 
00033 #include <ICLUtils/CompatMacros.h>
00034 #include <ICLUtils/Time.h>
00035 #include <ICLUtils/Range.h>
00036 #include <ICLUtils/ClippedCast.h>
00037 #include <cmath>
00038 #include <cstdlib>
00039 
00040 #ifdef WIN32
00041   #undef max
00042 #endif
00043 
00044 namespace icl{
00045   namespace utils{
00047 
00049     inline void randomSeed(long int seedval) {
00050   #ifdef WIN32
00051       srand(seedval);
00052   #else
00053       srand48(seedval);
00054   #endif
00055     }
00056   
00058     inline void randomSeed() { randomSeed(Time::now().toMicroSeconds()); }
00059   
00061 
00062     struct RandomSeeder{
00063       inline RandomSeeder(){randomSeed();}
00064     };
00065   
00067     inline double random(double max = 1) {
00068   #ifdef WIN32
00069       // this generates quite poor random numbers, because RAND_MAX = 32767
00070       return max*(static_cast<double>(rand()) / (1.0 + static_cast<double>(RAND_MAX)));
00071   #else
00072       return max*drand48();
00073   #endif
00074     }
00075     
00077 
00080     inline double random(double min, double max) {
00081       return ((max - min) * random() + min); 
00082     }
00083     
00085     template<class T>
00086     inline double random(const Range<T> &r){
00087       return random((double)r.minVal,(double)r.maxVal);
00088     }
00089    
00091 
00093     inline unsigned int random(unsigned int max) {
00094       unsigned int val = static_cast<unsigned int>(floor(random (static_cast<double>(max)+1.0)));
00095       return iclMin(val, max);
00096     }
00097     
00098   #if 0
00099     // removed due to lack of usebility: use std::fill(i.begin(c),i.end(c),URand(range)) instead
00100     
00102 
00106     ICLUtils_API void random(ImgBase *poImage, const Range<double> &range=Range<double>(0,255), bool roiOnly=true);
00107   
00109 
00115     ICLUtils_API void gaussRandom(ImgBase *poImage, double mean, double var, const Range<double> &minAndMax, bool roiOnly=true);
00116   #endif
00117 
00118 
00123     ICLUtils_API double gaussRandom(double mean, double var);
00124   
00126 
00132     inline double gaussRandom(double mean, double var, const Range<double> &range){
00133       return clip<double>( gaussRandom(mean,var), range.minVal, range.maxVal);
00134     }
00135   
00137 
00152     class URand{
00153       Range64f range;
00154       public:
00156       inline URand():range(0,1){};
00157       
00159       inline URand(icl64f min, icl64f max):range(min,max){}
00160   
00162       inline URand(const Range64f &range):range(range){};
00163   
00165       inline operator icl64f() const { return random(range.minVal,range.maxVal); }
00166     };
00167   
00169 
00170     class URandI{
00171       unsigned int max;
00172       public:
00174       inline URandI(unsigned int max):max(max){}
00175       
00177       inline operator unsigned int() const{ return random(max); }
00178     };
00179   
00181 
00182     class GRand{
00183       icl64f mean,var;
00184       public:
00186       inline GRand(icl64f mean=0, icl64f var=1.0):mean(mean),var(var){}
00187       
00189       inline operator icl64f() const { return gaussRandom(mean,var); }
00190     };
00191   
00193 
00194     class GRandClip{
00195       icl64f mean,var;
00196       Range64f range;
00197       public:
00199       inline GRandClip(icl64f mean, icl64f var,const Range64f &range):mean(mean),var(var),range(range){}
00200       
00202       inline operator icl64f() const { return gaussRandom(mean,var,range); }
00203     };
00204   
00205   
00206   /* }}} */
00207   
00208   
00209     
00210   } // namespace utils
00211 }
00212 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines