Files Classes Functions Hierarchy
00001 #ifndef RANDOM_H 00002 #define RANDOM_H 00003 00004 #include <cassert> 00005 #include <algorithm> 00006 #include <climits> 00007 #include <cmath> 00008 using namespace std; 00009 00010 #include <point.h> 00011 #include <typedefs.h> 00012 00013 00014 /* 00015 About 00016 00017 random00<> random10<> random01<> random11<> generate 00018 random numbers between 0 and 1. 00019 00020 The suffix on random indicates whether the end points 00021 of the interval are included or not. eg random11< > 00022 includes the start and end points of the interval in 00023 sampling. 00024 */ 00025 00034 class randomgenerator 00035 { 00036 public: 00037 00039 uintc operator()() const 00040 { return rand(); } 00041 00043 void randomize_start() 00044 { srand( time(NULL) ); } 00045 00047 void seed(uintc s=1); 00048 00050 template< typename Q > 00051 static boolc domain(point3<Q> const & p, boolc zero=true, boolc one=true); 00053 template< typename Q > 00054 static boolc domain(point2<Q> const & p, boolc zero=true, boolc one=true); 00056 template< typename T > 00057 static boolc domain(T const & x, boolc zero=true, boolc one=true); 00058 00059 }; 00060 00061 00062 00068 template<typename T = double , typename G = randomgenerator > 00069 class random11 00070 { 00072 uint nend; 00074 T delta; 00075 public: 00076 00078 G rg; 00079 00081 typedef T Ttype; 00083 typedef G Gtype; 00084 00087 random11( uintc nend_=LONG_MAX) 00088 : nend(nend_), delta(1) { delta /= (nend-1); } 00089 00091 void seed(uint s=1) 00092 { rg.seed(s); } 00093 00095 T const operator()() const 00096 { return T(rg() % nend)*delta; } 00097 00099 void operator()(T& x) const 00100 { x = (rg() % nend)*delta; } 00101 00103 uintc endpoint() const 00104 { return nend; } 00105 00106 }; 00107 00108 00114 template<typename T = double , typename G = randomgenerator > 00115 class random00 00116 { 00117 uint nend; 00118 G rg; 00119 T delta; 00120 public: 00121 00123 typedef T Ttype; 00125 typedef G Gtype; 00126 00129 random00(uintc nend_=LONG_MAX) 00130 : nend(nend_), delta(1) { delta /= (nend+1);} 00131 00133 void seed(uintc s=1) 00134 { rg.seed(s); } 00135 00137 T const operator()() const 00138 { return T( 1+(rg() % nend))*delta; } 00139 00141 void operator()(T& x) const 00142 { x = T( 1+(rg() % nend))*delta; } 00143 00145 uintc endpoint() const 00146 { return nend; } 00147 }; 00148 00155 template<typename T = double , typename G = randomgenerator > 00156 class random01 00157 { 00158 uint nend; 00159 G rg; 00160 T delta; 00161 public: 00162 00164 typedef T Ttype; 00166 typedef G Gtype; 00167 00170 random01(uintc nend_=LONG_MAX) 00171 : nend(nend_), delta(1) { delta /= nend; } 00172 00174 void seed(uintc s=1) 00175 { rg.seed(s); } 00176 00178 T const operator()() const 00179 { return ( T(1+(rg() % nend)) )*delta; } 00180 00182 void operator()(T& x) const 00183 { x = ( T(1+(rg() % nend)) )*delta; } 00184 00186 uintc endpoint() const 00187 { return nend; } 00188 }; 00189 00190 00197 template<typename T = double , typename G = randomgenerator > 00198 class random10 00199 { 00200 uint nend; 00201 G rg; 00202 T delta; 00203 public: 00204 00206 typedef T Ttype; 00208 typedef G Gtype; 00209 00212 random10(uintc nend_=LONG_MAX) 00213 : nend(nend_), delta(1) { delta /= nend; } 00214 00216 void seed(uintc s=1) 00217 { rg.seed(s); } 00218 00220 T const operator()() const 00221 { return T(rg() % nend)*delta; } 00222 00224 void operator()(T& x) const 00225 { x = (rg() % nend)*delta; } 00226 00228 uintc endpoint() const 00229 { return nend; } 00230 }; 00231 00235 template< typename RG=random11<> > 00236 class randompoint 00237 { 00238 public: 00239 00241 RG random; 00242 00244 randompoint() {} 00245 00247 randompoint(RG random_) 00248 : random(random_) {} 00249 00250 typedef point2< typename RG::Ttype > pt2; 00251 typedef point3< typename RG::Ttype > pt3; 00252 00254 pt2 makepoint2() 00255 { return pt2(random(),random()); } 00257 pt3 makepoint3() 00258 { return pt3(random(),random(),random()); } 00259 00260 }; 00261 00262 //---------------------------------------------------------- 00263 // Implementation 00264 00265 00266 template< typename T > 00267 boolc randomgenerator::domain(T const & x, boolc zero, boolc one) 00268 { 00269 //cout << SHOW(x) << endl; 00270 if (zero==false) 00271 assertreturnfalseN( x == (T)0 ); 00272 00273 if (one==false) 00274 assertreturnfalseN( x == (T)1 ); 00275 00276 assertreturnfalseN( x < (T)0 ); 00277 00278 assertreturnfalse( x <= (T)1 ); 00279 00280 return true; 00281 } 00282 00283 00284 template< typename Q > 00285 boolc randomgenerator::domain(point2<Q> const & p, boolc zero, boolc one) 00286 { 00287 bool res=true; 00288 if ( domain(p.x,zero,one) == false ) 00289 res=false; 00290 if ( domain(p.y,zero,one) == false ) 00291 res=false; 00292 00293 return res; 00294 } 00295 00296 template< typename Q > 00297 boolc randomgenerator::domain(point3<Q> const & p, boolc zero, boolc one) 00298 { 00299 bool res=true; 00300 res &= domain(p.x,zero,one); 00301 res &= domain(p.y,zero,one); 00302 res &= domain(p.z,zero,one); 00303 00304 return res; 00305 } 00306 00307 00308 #endif 00309 00310
1.5.8