MTwistEngine.h 3.52 KB
Newer Older
Lynn Garren's avatar
Lynn Garren committed
1
// $Id: MTwistEngine.h,v 1.5 2010/06/16 17:24:53 garren Exp $
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// -*- C++ -*-
//
// -----------------------------------------------------------------------
//                             HEP Random
//                        --- MTwistEngine ---
//                          class header file
// -----------------------------------------------------------------------
// A "fast, compact, huge-period generator" based on M. Matsumoto and 
// T. Nishimura, "Mersenne Twister: A 623-dimensionally equidistributed 
// uniform pseudorandom number generator", to appear in ACM Trans. on
// Modeling and Computer Simulation.  It is a twisted GFSR generator
// with a Mersenne-prime period of 2^19937-1, uniform on open interval (0,1)
// For further information, see http://www.math.keio.ac.jp/~matumoto/emt.html
// =======================================================================
// Ken Smith      - Started initial draft: 14th Jul 1998
//                - Optimized to get pow() out of flat() method: 21st Jul
//                - Added conversion operators:  6th Aug 1998
// M Fischler	  - Changes in way powers of two are kept: 16-Sep-1998
20
21
// Mark Fischler  - Methods for distrib. instance save/restore 12/8/04    
// Mark Fischler    methods for anonymous save/restore 12/27/04    
22
23
24
25
26
// =======================================================================

#ifndef MTwistEngine_h
#define MTwistEngine_h

Lynn Garren's avatar
Lynn Garren committed
27
#include "CLHEP/Random/defs.h"
28
29
#include "CLHEP/Random/RandomEngine.h"

Lynn Garren's avatar
Lynn Garren committed
30
31
namespace CLHEP {

32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
 * @author
 * @ingroup random
 */
class MTwistEngine : public HepRandomEngine {

public:

  MTwistEngine();
  MTwistEngine( long seed );
  MTwistEngine( int rowIndex, int colIndex );
  MTwistEngine( std::istream & is );
  virtual ~MTwistEngine();
  // Constructors and destructor.

  double flat();
  // Returns a pseudo random number between 0 and 1 (excluding the end points).

  void flatArray(const int size, double* vect);
  // Fills an array "vect" of specified size with flat random values.

  void setSeed(long seed, int);
  // Sets the state of the algorithm according to seed.

  void setSeeds(const long * seeds, int);
  // Sets the state of the algorithm according to the zero terminated
  // array of seeds. It is allowed to ignore one or many seeds in this array.

  void saveStatus( const char filename[] = "MTwist.conf") const;
  // Saves the current engine status in the named file

  void restoreStatus( const char filename[] = "MTwist.conf" );
  // Reads from named file the the last saved engine status and restores it.

  void showStatus() const;
  // Dumps the current engine status on the screen.

Lynn Garren's avatar
Lynn Garren committed
69
70
  operator double();       // Returns same as flat()
  operator float();        // returns flat, without worrying about filling bits
71
72
  operator unsigned int(); // 32-bit flat, quickest of all

73
74
75
76
  virtual std::ostream & put (std::ostream & os) const;
  virtual std::istream & get (std::istream & is);
  static  std::string beginTag ( );
  virtual std::istream & getState ( std::istream & is );
77

78
79
80
81
82
83
84
85
86
  std::string name() const;
  static std::string engineName() {return "MTwistEngine";}

  std::vector<unsigned long> put () const;
  bool get (const std::vector<unsigned long> & v);
  bool getState (const std::vector<unsigned long> & v);

  static const unsigned int VECTOR_STATE_SIZE = 626;
  
87
88
89
90
91
92
93
94
95
private:

  unsigned int mt[624];
  int count624;

  enum{ NminusM = 227, M = 397, N = 624};

}; // MTwistEngine

Lynn Garren's avatar
Lynn Garren committed
96
97
}  // namespace CLHEP

Lynn Garren's avatar
Lynn Garren committed
98
99
100
101
102
#ifdef ENABLE_BACKWARDS_COMPATIBILITY
//  backwards compatibility will be enabled ONLY in CLHEP 1.9
using namespace CLHEP;
#endif

103
#endif // MTwistEngine_h