Image Component Library (ICL)
|
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.GPL ** 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 <cmath> 00034 #include <cstdlib> 00035 #include <ICLUtils/Time.h> 00036 #include <ICLUtils/Range.h> 00037 #include <ICLUtils/ClippedCast.h> 00038 00039 namespace icl{ 00040 namespace utils{ 00042 00044 inline void randomSeed(long int seedval) { 00045 #ifdef ICL_SYSTEM_WINDOWS 00046 srand(seedval); 00047 #else 00048 srand48(seedval); 00049 #endif 00050 } 00051 00053 inline void randomSeed() {randomSeed(Time::now().toMicroSeconds());} 00054 00056 00057 struct RandomSeeder{ 00058 inline RandomSeeder(){randomSeed();} 00059 }; 00060 00062 inline double random(double max=1) { 00063 #ifdef WIN32 00064 // this generates quite poor random numbers, because RAND_MAX = 32767 00065 return max*(static_cast<double>(rand()) / (1.0 + static_cast<double>(RAND_MAX))); 00066 #else 00067 return max*drand48(); 00068 #endif 00069 } 00070 00072 00075 inline double random(double min, double max) { 00076 return ((max - min) * random() + min); 00077 } 00078 00080 template<class T> 00081 inline double random(const Range<T> &r){ 00082 return random((double)r.minVal,(double)r.maxVal); 00083 } 00084 00086 00088 inline unsigned int random(unsigned int max) { 00089 unsigned int val = static_cast<unsigned int>(floor(random (static_cast<double>(max)+1.0))); 00090 return iclMin(val, max); 00091 } 00092 00093 #if 0 00094 // removed due to lack of usebility: use std::fill(i.begin(c),i.end(c),URand(range)) instead 00095 00097 00101 void random(ImgBase *poImage, const Range<double> &range=Range<double>(0,255), bool roiOnly=true); 00102 00104 00110 void gaussRandom(ImgBase *poImage, double mean, double var, const Range<double> &minAndMax, bool roiOnly=true); 00111 #endif 00112 00113 00118 double gaussRandom(double mean, double var); 00119 00121 00127 inline double gaussRandom(double mean, double var, const Range<double> &range){ 00128 return clip<double>( gaussRandom(mean,var), range.minVal, range.maxVal); 00129 } 00130 00132 00147 class URand{ 00148 Range64f range; 00149 public: 00151 inline URand():range(0,1){}; 00152 00154 inline URand(icl64f min, icl64f max):range(min,max){} 00155 00157 inline URand(const Range64f &range):range(range){}; 00158 00160 inline operator icl64f() const { return random(range.minVal,range.maxVal); } 00161 }; 00162 00164 00165 class URandI{ 00166 unsigned int max; 00167 public: 00169 inline URandI(unsigned int max):max(max){} 00170 00172 inline operator unsigned int() const{ return random(max); } 00173 }; 00174 00176 00177 class GRand{ 00178 icl64f mean,var; 00179 public: 00181 inline GRand(icl64f mean=0, icl64f var=1.0):mean(mean),var(var){} 00182 00184 inline operator icl64f() const { return gaussRandom(mean,var); } 00185 }; 00186 00188 00189 class GRandClip{ 00190 icl64f mean,var; 00191 Range64f range; 00192 public: 00194 inline GRandClip(icl64f mean, icl64f var,const Range64f &range):mean(mean),var(var),range(range){} 00195 00197 inline operator icl64f() const { return gaussRandom(mean,var,range); } 00198 }; 00199 00200 00201 /* }}} */ 00202 00203 00204 00205 } // namespace utils 00206 } 00207