BasicRandom.hh

Go to the documentation of this file.
00001 // Simple random number generator class taken from nlojet++.
00002 // Some doxygen-style comments added by Gavin Salam, August 2006.
00003 // $Id: BasicRandom.hh 621 2007-05-09 10:34:30Z salam $
00004 //
00005 //  Copyright (C) 2002 Zoltan Nagy
00006 //
00007 //  This program is free software; you can redistribute it and/or modify
00008 //  it under the terms of the GNU General Public License as published by
00009 //  the Free Software Foundation; either version 2 of the License, or
00010 //  (at your option) any later version.
00011 //
00012 //  This program 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
00015 //  GNU General Public License for more details.
00016 //
00017 //  You should have received a copy of the GNU General Public License
00018 //  along with this program; if not, write to the Free Software
00019 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 #ifndef __FASTJET_BASICRANDOM_HH__
00021 #define __FASTJET_BASICRANDOM_HH__ 1
00022 
00023 //   Standard includes
00024 #include <iostream>
00025 #include <vector>
00026 #include <cassert>
00027 #include "fastjet/internal/base.hh"
00028 
00029 FASTJET_BEGIN_NAMESPACE      // defined in fastjet/internal/base.hh
00030 
00031 template<typename _Tp> class BasicRandom {
00032 public:
00033   typedef _Tp          value_type;
00034   typedef unsigned int size_type;
00035   typedef value_type*  pointer;
00036   
00037   //   give pseudo random numbers
00038   value_type operator() ();
00039   void       operator() (size_type, pointer);
00040 
00041   //   (re)initialize the random number generator
00042   void randomize(void *);
00043 
00044   //   minimum and maximum values
00045   static value_type min();
00046   static value_type max();
00047 
00048   //   print the informations about the generator to the stream
00049   void print_info(std::ostream& __os = std::cout);
00050 };
00051 
00052 //   default random generator
00053 int __default_random_generator(int *__iseed); 
00054 
00055 
00056 //   specializations
00057 template<>
00058 class BasicRandom<int>
00059 {
00060 public:
00061   typedef int          value_type;
00062   typedef unsigned int size_type;
00063   typedef value_type*  pointer;
00064   
00065   // constructors
00066   explicit BasicRandom(int __s1 = 12345, int __s2 = 67890) {
00067     _M_iseed[0] = __s1;
00068     _M_iseed[1] = __s2;
00069   }
00070     
00071   //   give pseudo random numbers
00072   value_type operator() () { 
00073     return __default_random_generator(_M_iseed);
00074   }
00075   
00076   void operator() (size_type __n, pointer __res) {
00077     for(size_type __i = 0; __i < __n; __i++) 
00078       __res[__i] = __default_random_generator(_M_iseed);
00079   }
00080 
00081   //   (re)initialize the random number generator
00082   void randomize(void *__iseed) {
00083     int *__new_seed = (int*) __iseed;
00084     _M_iseed[0] = __new_seed[0];
00085     _M_iseed[1] = __new_seed[1];
00086   }
00087 
00088   void set_status(const std::vector<int> & __iseed) {
00089     assert(__iseed.size() >= 2);
00090     _M_iseed[0] = __iseed[0];
00091     _M_iseed[1] = __iseed[1];
00092   }
00093 
00094   void get_status(std::vector<int> & __iseed) {
00095     __iseed.resize(2);
00096     __iseed[0] = _M_iseed[0];
00097     __iseed[1] = _M_iseed[1];
00098   }
00099   
00100   //   minimum and maximum values
00101   inline static value_type min() { return 0;}
00102   inline static value_type max() { return 2147483647;}
00103 
00104   //   print the informations about the generator to the stream
00105   void print_info(std::ostream& __os = std::cout) {
00106     __os<<"BasicRandom<int> : "<<_M_iseed[0]<<", "<<_M_iseed[1]<<std::endl;
00107   }
00108   
00109 private:
00110   int _M_iseed[2];
00111 };
00112   
00113 
00115 template<> class BasicRandom<double> {
00116 public:
00117   typedef double       value_type;
00118   typedef unsigned int size_type;
00119   typedef value_type*  pointer;
00120   
00122   explicit BasicRandom(int __s1 = 12345, int __s2 = 67890) {
00123     _M_iseed[0] = __s1;
00124     _M_iseed[1] = __s2;
00125   }
00126     
00129   value_type operator() () { 
00130     return 4.6566128752457969241e-10*__default_random_generator(_M_iseed);
00131   }
00132   
00135   void operator() (size_type __n, pointer __res) {
00136     for(size_type __i = 0; __i < __n; __i++) 
00137       __res[__i] = this -> operator()(); 
00138   }
00139 
00141   void randomize(void *__iseed) {
00142     int *__new_seed = (int*) __iseed;
00143     _M_iseed[0] = __new_seed[0];
00144     _M_iseed[1] = __new_seed[1];
00145   }
00146   
00147   void set_status(const std::vector<int> & __iseed) {
00148     assert(__iseed.size() >= 2);
00149     _M_iseed[0] = __iseed[0];
00150     _M_iseed[1] = __iseed[1];
00151   }
00152 
00153   void get_status(std::vector<int> & __iseed) {
00154     __iseed.resize(2);
00155     __iseed[0] = _M_iseed[0];
00156     __iseed[1] = _M_iseed[1];
00157   }
00158   
00160   inline static value_type min() { return 0.0;}
00162   inline static value_type max() { return 1.0;}
00163 
00165   void print_info(std::ostream& __os = std::cout) {
00166     __os<<"BasicRandom<double> : "<<_M_iseed[0]<<", "<<_M_iseed[1]<<std::endl;
00167   }
00168   
00169 private:
00170   int _M_iseed[2];
00171 };
00172   
00173 //   globally defined random number generator
00174 extern BasicRandom<int>     _G_random_int;
00175 extern BasicRandom<double>  _G_random_double;
00176 
00177 
00178 FASTJET_END_NAMESPACE
00179 
00180 #endif // __FASTJET_BASICRANDOM_HH__
00181 

Generated on Thu Jan 3 19:04:29 2008 for fastjet by  doxygen 1.5.2