CLHEP/Random/RandFlat.h

00001 // $Id: RandFlat.h,v 1.14 2002/04/12 15:02:44 evc Exp $
00002 // -*- C++ -*-
00003 //
00004 // -----------------------------------------------------------------------
00005 //                             HEP Random
00006 //                           --- RandFlat ---
00007 //                          class header file
00008 // -----------------------------------------------------------------------
00009 // This file is part of Geant4 (simulation toolkit for HEP).
00010 
00011 // Class defining methods for shooting flat random numbers, double or
00012 // integers.
00013 // It provides methods to fill with double flat values arrays of
00014 // specified size, as well as methods for shooting sequences of 0,1 (bits).
00015 // Default boundaries ]0.1[ for operator()().
00016 
00017 // =======================================================================
00018 // Gabriele Cosmo - Created: 5th September 1995
00019 // Peter Urban    - ShootBit() and related stuff added: 5th Sep 1996
00020 // Gabriele Cosmo - Added operator() and additional methods to fill
00021 //                  arrays specifying boundaries: 24th Jul 1997 
00022 // J.Marraffino   - Added default arguments as attributes and
00023 //                  operator() with arguments: 16th Feb 1998
00024 // M. Fischler    - Moved copy constructor to protected so that
00025 //                  derived RandBit can get at it.
00026 // =======================================================================
00027 
00028 #ifndef RandFlat_h
00029 #define RandFlat_h 1
00030 
00031 #include "CLHEP/Random/Random.h"
00032 
00037 class RandFlat : public HepRandom {
00038 
00039 public:
00040 
00041   inline RandFlat ( HepRandomEngine& anEngine );
00042   inline RandFlat ( HepRandomEngine& anEngine, double width );
00043   inline RandFlat ( HepRandomEngine& anEngine, double a, double b );
00044   inline RandFlat ( HepRandomEngine* anEngine );
00045   inline RandFlat ( HepRandomEngine* anEngine, double width );
00046   inline RandFlat ( HepRandomEngine* anEngine, double a, double b );
00047   // These constructors should be used to instantiate a RandFlat
00048   // distribution object defining a local engine for it.
00049   // The static generator will be skipped using the non-static methods
00050   // defined below.
00051   // If the engine is passed by pointer the corresponding engine object
00052   // will be deleted by the RandFlat destructor.
00053   // If the engine is passed by reference the corresponding engine object
00054   // will not be deleted by the RandFlat destructor.
00055 
00056   virtual ~RandFlat();
00057   // Destructor
00058 
00059   // Static methods to shoot random values using the static generator
00060 
00061   static  double shoot();
00062 
00063   static  inline double shoot( double width );
00064 
00065   static  inline double shoot( double a, double b );
00066 
00067   static  inline long shootInt( long n );
00068 
00069   static  inline long shootInt( long m, long n );
00070 
00071   static  inline int shootBit();
00072 
00073   static  void shootArray ( const int size, double* vect );
00074 
00075   static  void shootArray ( const int size, double* vect,
00076                             double lx, double dx );
00077 
00078   //  Static methods to shoot random values using a given engine
00079   //  by-passing the static generator.
00080 
00081   static  inline double shoot ( HepRandomEngine* anEngine );
00082 
00083   static  inline double shoot( HepRandomEngine* anEngine, double width );
00084 
00085   static  inline double shoot( HepRandomEngine* anEngine,
00086                                   double a, double b );
00087   static  inline long shootInt( HepRandomEngine* anEngine, long n );
00088   
00089   static  inline long shootInt( HepRandomEngine* anEngine, long m, long n );
00090   
00091   static  inline int shootBit( HepRandomEngine* );
00092 
00093   static  inline void shootArray ( HepRandomEngine* anEngine,
00094                                    const int size, double* vect );
00095 
00096   static  void shootArray ( HepRandomEngine* anEngine, 
00097                             const int size, double* vect,
00098                             double lx, double dx );
00099 
00100   //  Methods using the localEngine to shoot random values, by-passing
00101   //  the static generator.
00102 
00103   inline double fire();
00104 
00105   inline double fire( double width );
00106 
00107   inline double fire( double a, double b );
00108 
00109   inline long fireInt( long n );
00110 
00111   inline long fireInt( long m, long n );
00112 
00113   inline int fireBit();
00114 
00115   void fireArray (const int size, double* vect);
00116 
00117   void fireArray (const int size, double* vect,
00118                   double lx, double dx);
00119 
00120   double operator()();
00121   double operator()( double width );
00122   double operator()( double a, double b );
00123   
00124   // Methods overriding the base class static saveEngineStatus ones,
00125   // by adding extra data so that save in one program, then further shootBit()s
00126   // will produce the identical sequence to restore in another program, then
00127   // generating shootBit() randoms there
00128 
00129   static void saveEngineStatus( const char filename[] = "Config.conf" );
00130   // Saves to file the current status of the current engine.
00131 
00132   static void restoreEngineStatus( const char filename[] = "Config.conf" );
00133   // Restores a saved status (if any) for the current engine.
00134 
00135 protected:
00136 
00137   // Protected copy constructor. Defining it here disallows use by users.
00138   RandFlat(const RandFlat& d);
00139 
00140 private:
00141 
00142   // ShootBits generates an integer random number,
00143   // which is used by fireBit().
00144   // The number is stored in randomInt and firstUnusedBit
00145 
00146   inline void fireBits();
00147   static inline void shootBits();
00148   static inline void shootBits(HepRandomEngine*);
00149 
00150   // In MSB, the most significant bit of the integer random number
00151   // generated by ShootBits() is set.
00152   // Note:
00153   //   the number of significant bits must be chosen so that
00154   //   - an unsigned long can hold it
00155   //   - and it should be less than the number of bits returned 
00156   //     by Shoot() which are not affected by precision problems
00157   //     on _each_ architecture.
00158   //   (Aim: the random generators should be machine-independent).
00159 
00160   static const unsigned long MSB; 
00161   static const int MSBBits;
00162   // These two are set up in RandFlat.cc and need not be saved/restored
00163 
00164   unsigned long randomInt;
00165   unsigned long firstUnusedBit;
00166   static unsigned long staticRandomInt;
00167   static unsigned long staticFirstUnusedBit;
00168   
00169   HepRandomEngine* localEngine;
00170   bool deleteEngine;
00171   const double defaultWidth;
00172   const double defaultA;
00173   const double defaultB;
00174 
00175 };
00176 
00177 #include "CLHEP/Random/RandFlat.icc"
00178 
00179 #endif

Class Library for High Energy Physics (version 1.8)