]>
Commit | Line | Data |
---|---|---|
370be031 | 1 | // Simple random number generator class taken from nlojet++. |
2 | // Some doxygen-style comments added by Gavin Salam, August 2006. | |
3 | // $Id: BasicRandom.hh 621 2007-05-09 10:34:30Z salam $ | |
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 | namespace fastjet { // defined in fastjet/internal/base.hh | |
30 | ||
31 | template<typename _Tp> class BasicRandom { | |
32 | public: | |
33 | typedef _Tp value_type; | |
34 | typedef unsigned int size_type; | |
35 | typedef value_type* pointer; | |
36 | ||
37 | // give pseudo random numbers | |
38 | value_type operator() (); | |
39 | void operator() (size_type, pointer); | |
40 | ||
41 | // (re)initialize the random number generator | |
42 | void randomize(void *); | |
43 | ||
44 | // minimum and maximum values | |
45 | static value_type min(); | |
46 | static value_type max(); | |
47 | ||
48 | // print the informations about the generator to the stream | |
49 | void print_info(std::ostream& __os = std::cout); | |
50 | }; | |
51 | ||
52 | // default random generator | |
53 | int __default_random_generator(int *__iseed); | |
54 | ||
55 | ||
56 | // specializations | |
57 | template<> | |
58 | class BasicRandom<int> | |
59 | { | |
60 | public: | |
61 | typedef int value_type; | |
62 | typedef unsigned int size_type; | |
63 | typedef value_type* pointer; | |
64 | ||
65 | // constructors | |
66 | explicit BasicRandom(int __s1 = 12345, int __s2 = 67890) { | |
67 | _M_iseed[0] = __s1; | |
68 | _M_iseed[1] = __s2; | |
69 | } | |
70 | ||
71 | // give pseudo random numbers | |
72 | value_type operator() () { | |
73 | return __default_random_generator(_M_iseed); | |
74 | } | |
75 | ||
76 | void operator() (size_type __n, pointer __res) { | |
77 | for(size_type __i = 0; __i < __n; __i++) | |
78 | __res[__i] = __default_random_generator(_M_iseed); | |
79 | } | |
80 | ||
81 | // (re)initialize the random number generator | |
82 | void randomize(void *__iseed) { | |
83 | int *__new_seed = (int*) __iseed; | |
84 | _M_iseed[0] = __new_seed[0]; | |
85 | _M_iseed[1] = __new_seed[1]; | |
86 | } | |
87 | ||
88 | void set_status(const std::vector<int> & __iseed) { | |
89 | assert(__iseed.size() >= 2); | |
90 | _M_iseed[0] = __iseed[0]; | |
91 | _M_iseed[1] = __iseed[1]; | |
92 | } | |
93 | ||
94 | void get_status(std::vector<int> & __iseed) { | |
95 | __iseed.resize(2); | |
96 | __iseed[0] = _M_iseed[0]; | |
97 | __iseed[1] = _M_iseed[1]; | |
98 | } | |
99 | ||
100 | // minimum and maximum values | |
101 | inline static value_type min() { return 0;} | |
102 | inline static value_type max() { return 2147483647;} | |
103 | ||
104 | // print the informations about the generator to the stream | |
105 | void print_info(std::ostream& __os = std::cout) { | |
106 | __os<<"BasicRandom<int> : "<<_M_iseed[0]<<", "<<_M_iseed[1]<<std::endl; | |
107 | } | |
108 | ||
109 | private: | |
110 | int _M_iseed[2]; | |
111 | }; | |
112 | ||
113 | ||
114 | /// template specialization (double) for the BasicRandom template class. | |
115 | template<> class BasicRandom<double> { | |
116 | public: | |
117 | typedef double value_type; | |
118 | typedef unsigned int size_type; | |
119 | typedef value_type* pointer; | |
120 | ||
121 | /// constructor that takes two integers to specify the seed | |
122 | explicit BasicRandom(int __s1 = 12345, int __s2 = 67890) { | |
123 | _M_iseed[0] = __s1; | |
124 | _M_iseed[1] = __s2; | |
125 | } | |
126 | ||
127 | /// return a single pseudorandom double number, in the range 0.0 to 1.0 | |
128 | /// (not sure whether this range is open or closed) | |
129 | value_type operator() () { | |
130 | return 4.6566128752457969241e-10*__default_random_generator(_M_iseed); | |
131 | } | |
132 | ||
133 | /// given a pointer __res to the beginning of an array, fill that array | |
134 | /// with __n random numbers | |
135 | void operator() (size_type __n, pointer __res) { | |
136 | for(size_type __i = 0; __i < __n; __i++) | |
137 | __res[__i] = this -> operator()(); | |
138 | } | |
139 | ||
140 | /// (re)initialize the random number generator from an array of seeds | |
141 | void randomize(void *__iseed) { | |
142 | int *__new_seed = (int*) __iseed; | |
143 | _M_iseed[0] = __new_seed[0]; | |
144 | _M_iseed[1] = __new_seed[1]; | |
145 | } | |
146 | ||
147 | void set_status(const std::vector<int> & __iseed) { | |
148 | assert(__iseed.size() >= 2); | |
149 | _M_iseed[0] = __iseed[0]; | |
150 | _M_iseed[1] = __iseed[1]; | |
151 | } | |
152 | ||
153 | void get_status(std::vector<int> & __iseed) { | |
154 | __iseed.resize(2); | |
155 | __iseed[0] = _M_iseed[0]; | |
156 | __iseed[1] = _M_iseed[1]; | |
157 | } | |
158 | ||
159 | /// minimum value returned by the generator | |
160 | inline static value_type min() { return 0.0;} | |
161 | /// maximum value returned by the generator | |
162 | inline static value_type max() { return 1.0;} | |
163 | ||
164 | /// print information about the generator to the stream | |
165 | void print_info(std::ostream& __os = std::cout) { | |
166 | __os<<"BasicRandom<double> : "<<_M_iseed[0]<<", "<<_M_iseed[1]<<std::endl; | |
167 | } | |
168 | ||
169 | private: | |
170 | int _M_iseed[2]; | |
171 | }; | |
172 | ||
173 | // globally defined random number generator | |
174 | extern BasicRandom<int> _G_random_int; | |
175 | extern BasicRandom<double> _G_random_double; | |
176 | ||
177 | ||
178 | } // fastjet namespace | |
179 | ||
180 | #endif // __FASTJET_BASICRANDOM_HH__ | |
181 |