npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
AbsRandomGenerator.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_ABSRANDOMGENERATOR_HH_
2 #define NPSTAT_ABSRANDOMGENERATOR_HH_
3 
4 /*!
5 // \file AbsRandomGenerator.hh
6 //
7 // \brief Interface definition for pseudo- and quasi-random number generators
8 //
9 // Author: I. Volobouev
10 //
11 // March 2010
12 */
13 
14 #include <cassert>
15 #include <stdexcept>
16 
17 namespace npstat {
18  /**
19  // Interface class for pseudo- and quasi-random number generators.
20  //
21  // While implementing this interface, multivariate generators
22  // should override "run" and "operator()" while 1-d generators
23  // should just override "operator()". In multivariate contexts,
24  // one should always use "run" rather than "operator()".
25  */
27  {
28  inline virtual ~AbsRandomGenerator() {}
29 
30  /**
31  // Dimensionality of the generated vectors (or points).
32  // It is expected that "dim()" will always be 1 for
33  // pseudo-random (as opposed to quasi-random) generators.
34  */
35  virtual unsigned dim() const = 0;
36 
37  /**
38  // Maximum number of points which can be meaningfully produced
39  // by this generator (this is usually the generator period).
40  // If 0 is returned, it means that this number is larger than
41  // the maximum value of unsigned long long (which is typically
42  // 2^64 - 1). Override as necessary.
43  */
44  virtual unsigned long long maxPoints() const {return 0;}
45 
46  /**
47  // Standard 1-d generator function. It should generate
48  // a run-time error for essentially multivariate generators
49  // whose dimensionality is larger than 1.
50  */
51  virtual double operator()() = 0;
52 
53  /**
54  // Generate a bunch of points. Multivariate generators
55  // must override this. Here, the effective dimensionality
56  // of the generator is bufLen/nPt.
57  */
58  virtual void run(double* buf, const unsigned bufLen, const unsigned nPt)
59  {
60  if (nPt)
61  {
62  if (this->dim() != 1U) throw std::invalid_argument(
63  "In npstat::AbsRandomGenerator::run: this method "
64  "must be overriden by multivariate generators");
65  assert(buf);
66  for (unsigned i=0; i<bufLen; ++i)
67  buf[i] = this->operator()();
68  }
69  }
70  };
71 
72  /**
73  // Wrapper for functions which look like "double generate()",
74  // for example, "drand48" from cstdlib on Linux.
75  */
77  {
78  public:
79  inline explicit WrappedRandomGen(double (*fcn)()) : f_(fcn) {}
80  inline virtual ~WrappedRandomGen() {}
81 
82  inline unsigned dim() const {return 1U;}
83  inline double operator()() {return f_();}
84 
85  private:
87  double (*f_)();
88  };
89 }
90 
91 #endif // NPSTAT_ABSRANDOMGENERATOR_HH_
Definition: AbsRandomGenerator.hh:77
unsigned dim() const
Definition: AbsRandomGenerator.hh:82
double operator()()
Definition: AbsRandomGenerator.hh:83
Definition: AbsArrayProjector.hh:14
Definition: AbsRandomGenerator.hh:27
virtual void run(double *buf, const unsigned bufLen, const unsigned nPt)
Definition: AbsRandomGenerator.hh:58
virtual double operator()()=0
virtual unsigned long long maxPoints() const
Definition: AbsRandomGenerator.hh:44
virtual unsigned dim() const =0