00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00030 #ifdef _MSC_VER
00031 # pragma once
00032 # pragma warning( push )
00033 # pragma warning( disable : 4786 4512 )
00034 #endif // _MSC_VER
00035
00036 #ifndef EXMAT_MATH_RANDOM_H
00037 #define EXMAT_MATH_RANDOM_H
00038
00039 #include "../Mat.h"
00040 #include <cmath>
00041 #include <ctime>
00042
00043
00044 namespace exmat {
00045
00046
00048 class UniformRand {
00049 public:
00050 UniformRand(int seed=time(NULL))
00051 : min(0), max(1), invRandMax(1.0/RAND_MAX)
00052 {
00053 srand(seed);
00054 }
00055 UniformRand(double min_, double max_, int seed=time(NULL))
00056 : min(min_), max(max_), invRandMax(1.0/RAND_MAX)
00057 {
00058 if(max < min)
00059 throw 0;
00060 srand(seed);
00061 }
00062
00063 operator const int() const {
00064 return int(rand() * invRandMax * (int(max)-int(min)+1) ) + int(min);
00065 }
00066 operator const float() const {
00067 return float(rand() * invRandMax * (max-min) + min);
00068 }
00069 operator const double() const {
00070 return rand() * invRandMax * (max-min) + min;
00071 }
00072 private:
00073 const double min, max, invRandMax;
00074 };
00075
00076
00078 template<class Rep, class ErrChk, class RAN> EXMAT_INLINE0 void
00079 randomize(Mat<Rep,ErrChk>& mat, const RAN& ran) {
00080 typedef typename Rep::value_type value_type;
00081 typedef typename Rep::index_type index_type;
00082 for(index_type i=0; i<mat.rows(); ++i) for(index_type j=0; j<mat.cols(); ++j)
00083 mat.setAt(i, j) = value_type(ran);
00084 }
00085
00086
00087 }
00088
00089 #ifdef _MSC_VER
00090 # pragma warning( pop )
00091 #endif // _MSC_VER
00092
00093 #endif // EXMAT_MATH_RANDOM_H