FastJet  3.1.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
BasicRandom.hh
1 // Simple random number generator class taken from nlojet++.
2 // Some doxygen-style comments added by Gavin Salam, August 2006.
3 // $Id: BasicRandom.hh 1761 2010-09-16 10:43:18Z soyez $
4 //
5 // Copyright (C) 2002 Zoltan Nagy
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #ifndef __FASTJET_BASICRANDOM_HH__
21 #define __FASTJET_BASICRANDOM_HH__ 1
22 
23 // Standard includes
24 #include <iostream>
25 #include <vector>
26 #include <cassert>
27 #include "fastjet/internal/base.hh"
28 
29 FASTJET_BEGIN_NAMESPACE // defined in fastjet/internal/base.hh
30 
31 /// \if internal_doc
32 /// @ingroup internal
33 /// \class BasicRandom
34 /// Base class for random number generator of a generic value type
35 /// \endif
36 template<typename _Tp> class BasicRandom {
37 public:
38  typedef _Tp value_type;
39  typedef unsigned int size_type;
40  typedef value_type* pointer;
41 
42  // give pseudo random numbers
43  value_type operator() ();
44  void operator() (size_type, pointer);
45 
46  // (re)initialize the random number generator
47  void randomize(void *);
48 
49  // minimum and maximum values
50  static value_type min();
51  static value_type max();
52 
53  // print the informations about the generator to the stream
54  void print_info(std::ostream& __os = std::cout);
55 };
56 
57 // default random generator
58 int __default_random_generator(int *__iseed);
59 
60 
61 // specializations
62 
63 /// \if internal_doc
64 /// @ingroup internal
65 /// template specialization (int) for the BasicRandom template class.
66 /// \endif
67 template<>
68 class BasicRandom<int>
69 {
70 public:
71  typedef int value_type;
72  typedef unsigned int size_type;
73  typedef value_type* pointer;
74 
75  // constructors
76  explicit BasicRandom(int __s1 = 12345, int __s2 = 67890) {
77  _M_iseed[0] = __s1;
78  _M_iseed[1] = __s2;
79  }
80 
81  // give pseudo random numbers
82  value_type operator() () {
83  return __default_random_generator(_M_iseed);
84  }
85 
86  void operator() (size_type __n, pointer __res) {
87  for(size_type __i = 0; __i < __n; __i++)
88  __res[__i] = __default_random_generator(_M_iseed);
89  }
90 
91  // (re)initialize the random number generator
92  void randomize(void *__iseed) {
93  int *__new_seed = (int*) __iseed;
94  _M_iseed[0] = __new_seed[0];
95  _M_iseed[1] = __new_seed[1];
96  }
97 
98  void set_status(const std::vector<int> & __iseed) {
99  assert(__iseed.size() >= 2);
100  _M_iseed[0] = __iseed[0];
101  _M_iseed[1] = __iseed[1];
102  }
103 
104  void get_status(std::vector<int> & __iseed) {
105  __iseed.resize(2);
106  __iseed[0] = _M_iseed[0];
107  __iseed[1] = _M_iseed[1];
108  }
109 
110  // minimum and maximum values
111  inline static value_type min() { return 0;}
112  inline static value_type max() { return 2147483647;}
113 
114  // print the informations about the generator to the stream
115  void print_info(std::ostream& __os = std::cout) {
116  __os<<"BasicRandom<int> : "<<_M_iseed[0]<<", "<<_M_iseed[1]<<std::endl;
117  }
118 
119 private:
120  int _M_iseed[2];
121 };
122 
123 
124 /// \if internal_doc
125 /// @ingroup internal
126 /// template specialization (double) for the BasicRandom template class.
127 /// \endif
128 template<> class BasicRandom<double> {
129 public:
130  typedef double value_type;
131  typedef unsigned int size_type;
132  typedef value_type* pointer;
133 
134  /// constructor that takes two integers to specify the seed
135  explicit BasicRandom(int __s1 = 12345, int __s2 = 67890) {
136  _M_iseed[0] = __s1;
137  _M_iseed[1] = __s2;
138  }
139 
140  /// return a single pseudorandom double number, in the range 0.0 to 1.0
141  /// (not sure whether this range is open or closed)
142  value_type operator() () {
143  return 4.6566128752457969241e-10*__default_random_generator(_M_iseed);
144  }
145 
146  /// given a pointer __res to the beginning of an array, fill that array
147  /// with __n random numbers
148  void operator() (size_type __n, pointer __res) {
149  for(size_type __i = 0; __i < __n; __i++)
150  __res[__i] = this -> operator()();
151  }
152 
153  /// (re)initialize the random number generator from an array of seeds
154  void randomize(void *__iseed) {
155  int *__new_seed = (int*) __iseed;
156  _M_iseed[0] = __new_seed[0];
157  _M_iseed[1] = __new_seed[1];
158  }
159 
160  void set_status(const std::vector<int> & __iseed) {
161  assert(__iseed.size() >= 2);
162  _M_iseed[0] = __iseed[0];
163  _M_iseed[1] = __iseed[1];
164  }
165 
166  void get_status(std::vector<int> & __iseed) {
167  __iseed.resize(2);
168  __iseed[0] = _M_iseed[0];
169  __iseed[1] = _M_iseed[1];
170  }
171 
172  /// minimum value returned by the generator
173  inline static value_type min() { return 0.0;}
174  /// maximum value returned by the generator
175  inline static value_type max() { return 1.0;}
176 
177  /// print information about the generator to the stream
178  void print_info(std::ostream& __os = std::cout) {
179  __os<<"BasicRandom<double> : "<<_M_iseed[0]<<", "<<_M_iseed[1]<<std::endl;
180  }
181 
182 private:
183  int _M_iseed[2];
184 };
185 
186 // globally defined random number generator
187 extern BasicRandom<int> _G_random_int;
188 extern BasicRandom<double> _G_random_double;
189 
190 
191 FASTJET_END_NAMESPACE
192 
193 #endif // __FASTJET_BASICRANDOM_HH__
194