FastJet 3.0.2
BasicRandom.hh
00001 // Simple random number generator class taken from nlojet++.
00002 // Some doxygen-style comments added by Gavin Salam, August 2006.
00003 // $Id: BasicRandom.hh 1761 2010-09-16 10:43:18Z soyez $
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 /// \if internal_doc
00032 /// @ingroup internal
00033 /// \class BasicRandom
00034 /// Base class for random number generator of a generic value type
00035 /// \endif
00036 template<typename _Tp> class BasicRandom {
00037 public:
00038   typedef _Tp          value_type;
00039   typedef unsigned int size_type;
00040   typedef value_type*  pointer;
00041   
00042   //   give pseudo random numbers
00043   value_type operator() ();
00044   void       operator() (size_type, pointer);
00045 
00046   //   (re)initialize the random number generator
00047   void randomize(void *);
00048 
00049   //   minimum and maximum values
00050   static value_type min();
00051   static value_type max();
00052 
00053   //   print the informations about the generator to the stream
00054   void print_info(std::ostream& __os = std::cout);
00055 };
00056 
00057 //   default random generator
00058 int __default_random_generator(int *__iseed); 
00059 
00060 
00061 //   specializations
00062 
00063 /// \if internal_doc
00064 /// @ingroup internal
00065 /// template specialization (int) for the BasicRandom template class. 
00066 /// \endif
00067 template<>
00068 class BasicRandom<int>
00069 {
00070 public:
00071   typedef int          value_type;
00072   typedef unsigned int size_type;
00073   typedef value_type*  pointer;
00074   
00075   // constructors
00076   explicit BasicRandom(int __s1 = 12345, int __s2 = 67890) {
00077     _M_iseed[0] = __s1;
00078     _M_iseed[1] = __s2;
00079   }
00080     
00081   //   give pseudo random numbers
00082   value_type operator() () { 
00083     return __default_random_generator(_M_iseed);
00084   }
00085   
00086   void operator() (size_type __n, pointer __res) {
00087     for(size_type __i = 0; __i < __n; __i++) 
00088       __res[__i] = __default_random_generator(_M_iseed);
00089   }
00090 
00091   //   (re)initialize the random number generator
00092   void randomize(void *__iseed) {
00093     int *__new_seed = (int*) __iseed;
00094     _M_iseed[0] = __new_seed[0];
00095     _M_iseed[1] = __new_seed[1];
00096   }
00097 
00098   void set_status(const std::vector<int> & __iseed) {
00099     assert(__iseed.size() >= 2);
00100     _M_iseed[0] = __iseed[0];
00101     _M_iseed[1] = __iseed[1];
00102   }
00103 
00104   void get_status(std::vector<int> & __iseed) {
00105     __iseed.resize(2);
00106     __iseed[0] = _M_iseed[0];
00107     __iseed[1] = _M_iseed[1];
00108   }
00109   
00110   //   minimum and maximum values
00111   inline static value_type min() { return 0;}
00112   inline static value_type max() { return 2147483647;}
00113 
00114   //   print the informations about the generator to the stream
00115   void print_info(std::ostream& __os = std::cout) {
00116     __os<<"BasicRandom<int> : "<<_M_iseed[0]<<", "<<_M_iseed[1]<<std::endl;
00117   }
00118   
00119 private:
00120   int _M_iseed[2];
00121 };
00122   
00123 
00124 /// \if internal_doc
00125 /// @ingroup internal
00126 /// template specialization (double) for the BasicRandom template class. 
00127 /// \endif
00128 template<> class BasicRandom<double> {
00129 public:
00130   typedef double       value_type;
00131   typedef unsigned int size_type;
00132   typedef value_type*  pointer;
00133   
00134   /// constructor that takes two integers to specify the seed
00135   explicit BasicRandom(int __s1 = 12345, int __s2 = 67890) {
00136     _M_iseed[0] = __s1;
00137     _M_iseed[1] = __s2;
00138   }
00139     
00140   /// return a single pseudorandom double number, in the range 0.0 to 1.0
00141   /// (not sure whether this range is open or closed)
00142   value_type operator() () { 
00143     return 4.6566128752457969241e-10*__default_random_generator(_M_iseed);
00144   }
00145   
00146   /// given a pointer __res to the beginning of an array, fill that array
00147   /// with __n random numbers
00148   void operator() (size_type __n, pointer __res) {
00149     for(size_type __i = 0; __i < __n; __i++) 
00150       __res[__i] = this -> operator()(); 
00151   }
00152 
00153   ///  (re)initialize the random number generator from an array of seeds
00154   void randomize(void *__iseed) {
00155     int *__new_seed = (int*) __iseed;
00156     _M_iseed[0] = __new_seed[0];
00157     _M_iseed[1] = __new_seed[1];
00158   }
00159   
00160   void set_status(const std::vector<int> & __iseed) {
00161     assert(__iseed.size() >= 2);
00162     _M_iseed[0] = __iseed[0];
00163     _M_iseed[1] = __iseed[1];
00164   }
00165 
00166   void get_status(std::vector<int> & __iseed) {
00167     __iseed.resize(2);
00168     __iseed[0] = _M_iseed[0];
00169     __iseed[1] = _M_iseed[1];
00170   }
00171   
00172   /// minimum value returned by the generator
00173   inline static value_type min() { return 0.0;}
00174   /// maximum value returned by the generator
00175   inline static value_type max() { return 1.0;}
00176 
00177   ///  print information about the generator to the stream
00178   void print_info(std::ostream& __os = std::cout) {
00179     __os<<"BasicRandom<double> : "<<_M_iseed[0]<<", "<<_M_iseed[1]<<std::endl;
00180   }
00181   
00182 private:
00183   int _M_iseed[2];
00184 };
00185   
00186 //   globally defined random number generator
00187 extern BasicRandom<int>     _G_random_int;
00188 extern BasicRandom<double>  _G_random_double;
00189 
00190 
00191 FASTJET_END_NAMESPACE
00192 
00193 #endif // __FASTJET_BASICRANDOM_HH__
00194 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends