Random.h

Go to the documentation of this file.
00001 /*
00002  * Expression Template Matrix Library
00003  *
00004  * Copyright (C) 2004 - 2006 Ricky Lung <mtlung@users.sourceforge.net>
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with this library; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 }   // namespace exmat
00088 
00089 #ifdef _MSC_VER
00090 #   pragma warning( pop )
00091 #endif  // _MSC_VER
00092 
00093 #endif  // EXMAT_MATH_RANDOM_H

Generated on Sat May 6 23:11:59 2006 for Exmat by  doxygen 1.4.6-NO