Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

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 296 2006-08-19 10:20:49Z 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 "fastjet/internal/base.hh"
00026 
00027 FASTJET_BEGIN_NAMESPACE      // defined in fastjet/internal/base.hh
00028 
00029 template<typename _Tp> class BasicRandom {
00030 public:
00031   typedef _Tp          value_type;
00032   typedef unsigned int size_type;
00033   typedef value_type*  pointer;
00034   
00035   //   give pseudo random numbers
00036   value_type operator() ();
00037   void       operator() (size_type, pointer);
00038 
00039   //   (re)initialize the random number generator
00040   void randomize(void *);
00041 
00042   //   minimum and maximum values
00043   static value_type min();
00044   static value_type max();
00045 
00046   //   print the informations about the generator to the stream
00047   void print_info(std::ostream& __os = std::cout);
00048 };
00049 
00050 //   default random generator
00051 int __default_random_generator(int *__iseed); 
00052 
00053 
00054 //   specializations
00055 template<>
00056 class BasicRandom<int>
00057 {
00058 public:
00059   typedef int          value_type;
00060   typedef unsigned int size_type;
00061   typedef value_type*  pointer;
00062   
00063   // constructors
00064   explicit BasicRandom(int __s1 = 12345, int __s2 = 67890) {
00065     _M_iseed[0] = __s1;
00066     _M_iseed[1] = __s2;
00067   }
00068     
00069   //   give pseudo random numbers
00070   value_type operator() () { 
00071     return __default_random_generator(_M_iseed);
00072   }
00073   
00074   void operator() (size_type __n, pointer __res) {
00075     for(size_type __i = 0; __i < __n; __i++) 
00076       __res[__i] = __default_random_generator(_M_iseed);
00077   }
00078 
00079   //   (re)initialize the random number generator
00080   void randomize(void *__iseed) {
00081     int *__new_seed = (int*) __iseed;
00082     _M_iseed[0] = __new_seed[0];
00083     _M_iseed[1] = __new_seed[1];
00084   }
00085   
00086   //   minimum and maximum values
00087   inline static value_type min() { return 0;}
00088   inline static value_type max() { return 2147483647;}
00089 
00090   //   print the informations about the generator to the stream
00091   void print_info(std::ostream& __os = std::cout) {
00092     __os<<"BasicRandom<int> : "<<_M_iseed[0]<<", "<<_M_iseed[1]<<std::endl;
00093   }
00094   
00095 private:
00096   int _M_iseed[2];
00097 };
00098   
00099 
00101 template<> class BasicRandom<double> {
00102 public:
00103   typedef double       value_type;
00104   typedef unsigned int size_type;
00105   typedef value_type*  pointer;
00106   
00108   explicit BasicRandom(int __s1 = 12345, int __s2 = 67890) {
00109     _M_iseed[0] = __s1;
00110     _M_iseed[1] = __s2;
00111   }
00112     
00115   value_type operator() () { 
00116     return 4.6566128752457969241e-10*__default_random_generator(_M_iseed);
00117   }
00118   
00121   void operator() (size_type __n, pointer __res) {
00122     for(size_type __i = 0; __i < __n; __i++) 
00123       __res[__i] = this -> operator()(); 
00124   }
00125 
00127   void randomize(void *__iseed) {
00128     int *__new_seed = (int*) __iseed;
00129     _M_iseed[0] = __new_seed[0];
00130     _M_iseed[1] = __new_seed[1];
00131   }
00132   
00134   inline static value_type min() { return 0.0;}
00136   inline static value_type max() { return 1.0;}
00137 
00139   void print_info(std::ostream& __os = std::cout) {
00140     __os<<"BasicRandom<double> : "<<_M_iseed[0]<<", "<<_M_iseed[1]<<std::endl;
00141   }
00142   
00143 private:
00144   int _M_iseed[2];
00145 };
00146   
00147 //   globally defined random number generator
00148 extern BasicRandom<int>     _G_random_int;
00149 extern BasicRandom<double>  _G_random_double;
00150 
00151 
00152 FASTJET_END_NAMESPACE
00153 
00154 #endif // __FASTJET_BASICRANDOM_HH__
00155 

Generated on Mon Apr 2 20:57:48 2007 for fastjet by  doxygen 1.4.2