EGSnrc C++ class library  Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
egs_rndm.h
Go to the documentation of this file.
1 /*
2 ###############################################################################
3 #
4 # EGSnrc egs++ random number generators headers
5 # Copyright (C) 2015 National Research Council Canada
6 #
7 # This file is part of EGSnrc.
8 #
9 # EGSnrc is free software: you can redistribute it and/or modify it under
10 # the terms of the GNU Affero General Public License as published by the
11 # Free Software Foundation, either version 3 of the License, or (at your
12 # option) any later version.
13 #
14 # EGSnrc is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
17 # more details.
18 #
19 # You should have received a copy of the GNU Affero General Public License
20 # along with EGSnrc. If not, see <http://www.gnu.org/licenses/>.
21 #
22 ###############################################################################
23 #
24 # Author: Iwan Kawrakow, 2005
25 #
26 # Contributors:
27 #
28 ###############################################################################
29 */
30 
31 
37 #ifndef EGS_RANDOM_GENERATOR_
38 #define EGS_RANDOM_GENERATOR_
39 
40 #include "egs_libconfig.h"
41 #include "egs_math.h"
42 #include "egs_functions.h"
43 
44 class EGS_Input;
45 
68 
69 public:
70 
78  EGS_RandomGenerator(int n=128);
79 
86  copyBaseState(r);
87  };
88 
94  delete [] rarray;
95  };
96 
103  inline EGS_Float getUniform() {
104  if (ip >= np) {
105  fillArray(np,rarray);
106  ip = 0;
107  }
108  return rarray[ip++];
109  };
110 
116  EGS_I64 numbersGenerated() const {
117  return count;
118  };
119 
125  EGS_I64 numbersUsed() const {
126  return ip<np ? count - np + ip : count;
127  };
128 
138  inline void getAzimuth(EGS_Float &cphi, EGS_Float &sphi) {
139 #ifndef FAST_SINCOS
140  register EGS_Float xphi,xphi2,yphi,yphi2,rhophi;
141  do {
142  xphi = 2*getUniform() - 1;
143  xphi2 = xphi*xphi;
144  yphi = getUniform();
145  yphi2 = yphi*yphi;
146  rhophi = xphi2 + yphi2;
147  }
148  while (rhophi > 1);
149  cphi = (xphi2 - yphi2)/rhophi;
150  sphi = 2*xphi*yphi/rhophi;
151 #else
152  EGS_Float phi = 2*M_PI*getUniform();
153  cphi = cos(phi);
154  sphi = sin(phi);
155 #endif
156  };
157 
161  inline EGS_Float getGaussian() {
162  if (have_x) {
163  have_x = false;
164  return the_x;
165  }
166  EGS_Float r = sqrt(-2*log(1-getUniform()));
167  EGS_Float cphi, sphi;
168  getAzimuth(cphi,sphi);
169  have_x = true;
170  the_x = r*sphi;
171  return r*cphi;
172  };
173 
188  static EGS_RandomGenerator *createRNG(EGS_Input *inp, int sequence=0);
189 
196  static EGS_RandomGenerator *defaultRNG(int sequence=0);
197 
209  virtual void fillArray(int n, EGS_Float *array) = 0;
210 
212 
227  bool storeState(ostream &data);
228  bool setState(istream &data);
229  bool addState(istream &data);
230  void resetCounter() {
231  count = 0;
232  };
234 
236  virtual EGS_RandomGenerator *getCopy() = 0;
237 
239  virtual void setState(EGS_RandomGenerator *r) = 0;
240 
242  virtual void saveState() = 0;
243 
245  virtual void resetState() = 0;
246 
248  virtual int rngSize() const = 0;
249 
256  virtual void describeRNG() const {};
257 
258 protected:
259 
260  EGS_I64 count;
261  int np;
262  int ip;
263  EGS_Float *rarray;
264 
274  virtual bool storePrivateState(ostream &data) = 0;
275 
283  virtual bool setPrivateState(istream &data) = 0;
284 
285  void copyBaseState(const EGS_RandomGenerator &r);
286 
287  void allocate(int n);
288 
290  int baseSize() const {
291  return 2*sizeof(EGS_I32) + sizeof(EGS_I64) + sizeof(bool) +
292  sizeof(EGS_Float) + np*sizeof(EGS_Float);
293  };
294 
295 
296 private:
297 
298  bool have_x;
299  EGS_Float the_x;
300 
301 };
302 
303 #endif
#define EGS_EXPORT
Export symbols from the egspp library.
Definition: egs_libconfig.h:90
virtual ~EGS_RandomGenerator()
Destructor.
Definition: egs_rndm.h:93
void getAzimuth(EGS_Float &cphi, EGS_Float &sphi)
Sets cphi and sphi to the cosine and sine of a random angle uniformely distributed between 0 and ...
Definition: egs_rndm.h:138
int baseSize() const
The memory needed to store the base state.
Definition: egs_rndm.h:290
Global egspp functions header file.
EGS_I64 numbersUsed() const
Returns the number of random numbers used so far.
Definition: egs_rndm.h:125
EGS_RandomGenerator(const EGS_RandomGenerator &r)
Copy constructor.
Definition: egs_rndm.h:85
Base random number generator class. All random number generators should be derived from this class...
Definition: egs_rndm.h:67
EGS_I64 numbersGenerated() const
Returns the number of random numbers generated so far.
Definition: egs_rndm.h:116
Attempts to fix broken math header files.
EGS_Float getGaussian()
Returns a Gaussian distributed random number with mean zero and standard deviation 1...
Definition: egs_rndm.h:161
virtual void describeRNG() const
Describe this RNG.
Definition: egs_rndm.h:256
EGS_Float getUniform()
Returns a random number uniformly distributed between zero (inclusive) and 1 (exclusive).
Definition: egs_rndm.h:103
A class for storing information in a tree-like structure of key-value pairs. This class is used throu...
Definition: egs_input.h:182
int ip
pointer to the next rarray element in the sequence
Definition: egs_rndm.h:262
Defines the EGS_EXPORT and EGS_LOCAL macros.
int np
size of the array rarray
Definition: egs_rndm.h:261
EGS_Float * rarray
array with random numbers of size np.
Definition: egs_rndm.h:263