SourceForge Logo Tiny Vector Matrix library using Expression Templates Sourceforge Project Page

include/tvmet/util/Random.h

Go to the documentation of this file.
00001 /*
00002  * Tiny Vector Matrix Library
00003  * Dense Vector Matrix Libary of Tiny size using Expression Templates
00004  *
00005  * Copyright (C) 2001 - 2007 Olaf Petzold <opetzold@users.sourceforge.net>
00006  *
00007  * This library is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  *
00021  * $Id: Random.h,v 1.7 2007-06-23 15:58:59 opetzold Exp $
00022  */
00023 
00024 #ifndef TVMET_UTIL_RANDOM_H
00025 #define TVMET_UTIL_RANDOM_H
00026 
00027 #include <tvmet/CompileTimeError.h>
00028 
00029 namespace tvmet {
00030 
00031 namespace util {
00032 
00033 
00051 template<class T, int MIN=0, int MAX=100>
00052 class Random {
00053   static unsigned int       s_seed;
00054 public:
00055   typedef T         value_type;
00056   Random() { TVMET_CT_CONDITION(MIN<MAX, wrong_random_range) }
00057   value_type operator()() {
00058     s_seed += (unsigned)std::time(0);
00059     std::srand(s_seed);
00060     return MIN + int(double(MAX) * std::rand()/(double(RAND_MAX)+1.0));
00061   }
00062 };
00063 // instance
00064 template<class T, int MIN, int MAX>
00065 unsigned int Random<T, MIN, MAX>::s_seed;
00066 
00067 
00068 #if defined(TVMET_HAVE_COMPLEX)
00069 
00074 template<class T, int MIN=0, int MAX=100>
00075 class Random {
00076   static unsigned int       s_seed;
00077 public:
00078   typedef std::complex<T>     value_type;
00079   Random() { TVMET_CT_CONDITION(MIN<MAX, wrong_random_range) }
00080   value_type operator()() {
00081     s_seed += (unsigned)std::time(0);
00082     std::srand(s_seed);
00083     return MIN + int(double(MAX) * std::rand()/(double(RAND_MAX)+1.0));
00084   }
00085 };
00086 // instance
00087 template<class T, int MIN, int MAX>
00088 unsigned int Random<std::complex<T>, MIN, MAX>::s_seed;
00089 #endif // defined(TVMET_HAVE_COMPLEX)
00090 
00091 
00092 } // namespace util
00093 
00094 } // namespace tvmet
00095 
00096 #endif // TVMET_UTIL_RANDOM_H
00097 
00098 // Local Variables:
00099 // mode:C++
00100 // tab-width:8
00101 // End:

Author: