1 // $Id: RandEngine.h,v 1.6 2010/06/16 17:24:53 garren Exp $
2 // -*- C++ -*-
3 //
4 // -----------------------------------------------------------------------
5 //                             HEP Random
6 //                         --- RandEngine ---
7 //                          class header file
8 // -----------------------------------------------------------------------
9 // This file is part of Geant4 (simulation toolkit for HEP).
10 //
11 // Simple random engine using rand() and srand() functions from C standard
12 // library to implement the flat() basic distribution and for setting
13 // seeds.
14 // Copy constructor and operator= are private for objects of this class.
15 //
16 // WARNING: rand is not thread safe. If you need to use multiple
17 // engine objects on different threads concurrently, do not use RandEngine
18 
19 // =======================================================================
20 // Gabriele Cosmo - Created: 5th September 1995
21 //                - Minor corrections: 31st October 1996
22 //                - Added methods for engine status: 19th November 1996
23 //                - setSeed(), setSeeds() now have default dummy argument
24 //                  set to zero: 11th July 1997
25 //                - Private copy constructor and operator=: 26th Feb 1998
26 // J.Marraffino   - Added stream operators and related constructor.
27 //                  Added automatic seed selection from seed table and
28 //                  engine counter: 15th Feb 1998
29 // Ken Smith      - Added conversion operators:  6th Aug 1998
30 //                  replace mx by mantissa_bit_32
31 // M Fischler     - Inserted warnings about the fact that the quality of rand()
32 //                  is quite poor.
33 // Mark Fischler    Methods for distrib. instance save/restore 12/8/04
34 // Mark Fischler    methods for anonymous save/restore 12/27/04
35 // =======================================================================
36 
37 #ifndef RandEngine_h
38 #define RandEngine_h 1
39 
40 #include "CLHEP/Random/defs.h"
41 #include "CLHEP/Random/RandomEngine.h"
42 
43 namespace CLHEP {
44 
45 /**
46  * @author <Gabriele.Cosmo@cern.ch>
47  * @ingroup random
48  */
49 class RandEngine : public HepRandomEngine {
50 
51 public:
52 
53   RandEngine(std::istream& is);
54   RandEngine();
55   RandEngine(long seed);
56   RandEngine(int rowIndex, int colIndex);
57   virtual ~RandEngine();
58   // Constructors and destructor
59 
60   double flat();
61   // It returns a pseudo random number between 0 and 1,
62   // according to the standard stdlib random function rand()
63   // but excluding the end points.
64   //
65   // WARNING:  rand() is quite a weak generator on most systems,                    <
66   // will not pass several randomness tests, and does not give a           <
67   // reproducible sequence of numbers.                                                                        <
68 
69   void flatArray (const int size, double* vect);
70   // Fills the array "vect" of specified size with flat random values.
71 
72   void setSeed(long seed, int dum=0);
73   // Sets the state of the algorithm according to seed.
74 
75   void setSeeds(const long * seeds, int dum=0);
76   // Sets the state of the algorithm according to the zero terminated
77   // array of seeds. Only the first seed is used.
78 
79   void saveStatus( const char filename[] = "Rand.conf" ) const;
80   // Saves on file Rand.conf the current engine status.
81   // WARNING:  This is non-functional, as rand() on various systems will            <
82   // not give reproducible streams.                                                 <
83 
84   void restoreStatus( const char filename[] = "Rand.conf" );
85   // Reads from file Rand.conf the last saved engine status
86   // and restores it.
87   // WARNING:  This is non-functional, as rand() on various systems will            <
88   // not give reproducible streams.                                                 <
89 
90   void showStatus() const;
91   // Dumps the engine status on the screen.
92 
93   operator double();       // Returns same as flat()
94   operator float();        // flat value, without worrying about filling bits
95   operator unsigned int(); // 32-bit flat value, quickest of all.
96 
97   virtual std::ostream & put (std::ostream & os) const;
98   virtual std::istream & get (std::istream & is);
99   static  std::string beginTag ( );
100   virtual std::istream & getState ( std::istream & is );
101 
102   std::string name() const;
engineName()103   static std::string engineName() {return "RandEngine";}
104 
105   std::vector<unsigned long> put () const;
106   bool get (const std::vector<unsigned long> & v);
107   bool getState (const std::vector<unsigned long> & v);
108 
109   static const unsigned int VECTOR_STATE_SIZE = 3;
110 
111 private:
112 
113   RandEngine(const RandEngine &p);
114   RandEngine & operator = (const RandEngine &p);
115   // Private copy constructor and assignment operator.
116 
117 private:
118 
119   long seq;
120   static int numEngines;
121   static const int maxIndex;
122 
123 };
124 
125 }  // namespace CLHEP
126 
127 #ifdef ENABLE_BACKWARDS_COMPATIBILITY
128 //  backwards compatibility will be enabled ONLY in CLHEP 1.9
129 using namespace CLHEP;
130 #endif
131 
132 #endif
133