|
|
|||
File indexing completed on 2026-08-06 09:38:31
0001 // -*- C++ -*- 0002 // 0003 // UseRandom.h is a part of ThePEG - Toolkit for HEP Event Generation 0004 // Copyright (C) 1999-2019 Leif Lonnblad 0005 // 0006 // ThePEG is licenced under version 3 of the GPL, see COPYING for details. 0007 // Please respect the MCnet academic guidelines, see GUIDELINES for details. 0008 // 0009 #ifndef ThePEG_UseRandom_H 0010 #define ThePEG_UseRandom_H 0011 // This is the declaration of the UseRandom class. 0012 0013 #include "ThePEG/Repository/RandomGenerator.h" 0014 0015 namespace ThePEG { 0016 0017 /** 0018 * This UseRandom class keeps a static stack of RandomGenerator 0019 * objects which can be used anywhere by any class. When an 0020 * EventGenerator is initialized or run it adds a RandomGenerator 0021 * object to the stack which can be used by any other object being 0022 * initialized or run through the static functions of the UseRandom 0023 * class. If someone needs to use an alternative RandomGenerator 0024 * object a new UseRandom object can be constructed with a pointer to 0025 * the desired RandomGenerator object as argument and that object will 0026 * the be used by the static UseRandom functions until the UseRandom 0027 * object is destructed. 0028 * 0029 * @see RandomGenerator 0030 * @see EventGenerator 0031 * 0032 */ 0033 class UseRandom { 0034 0035 public: 0036 0037 /** 0038 * Default constructor does nothing. 0039 */ 0040 UseRandom() : randomPushed(false) {} 0041 0042 /** 0043 * Copy-constructor does nothing. 0044 */ 0045 UseRandom(const UseRandom &) : randomPushed(false) {} 0046 0047 /** 0048 * Construct a new object specifying a new RandomGenerator, \a r, to 0049 * be used during this objects lifetime 0050 */ 0051 UseRandom(const RanGenPtr & r) : randomPushed(false) { 0052 if ( r ) { 0053 theRandomStack.push_back(r); 0054 randomPushed = true; 0055 } 0056 } 0057 0058 /** 0059 * The destructor removing the RandomGenerator specified in the 0060 * constructor from the stack. 0061 */ 0062 ~UseRandom() { if ( randomPushed ) theRandomStack.pop_back(); } 0063 0064 public: 0065 0066 /** 0067 * Return a reference to the currently chosen RandomGenerator object. 0068 */ 0069 static RandomGenerator & current() { return *theRandomStack.back(); } 0070 0071 /** 0072 * Return a pointer to the currently chosen RandomGenerator object. 0073 */ 0074 // static RandomEngine * currentEngine() { 0075 // return &(current().randomGenerator()); 0076 // } 0077 0078 /** 0079 * Return a simple flat random number (from the current 0080 * RandomGenerator object) in the range ]0,1[. 0081 */ 0082 static double rnd() { return current().rnd(); } 0083 0084 /** 0085 * Return \a n simple flat random number (from the current 0086 * RandomGenerator object) in the range ]0,1[. 0087 */ 0088 static RandomGenerator::RndVector rndvec(int n) { 0089 return current().rndvec(n); 0090 } 0091 0092 /** 0093 * Return a simple flat random number (from the current 0094 * RandomGenerator object) in the range ]0,\a xu[. 0095 */ 0096 template <typename Unit> 0097 static Unit rnd(Unit xu) { return current().rnd(xu); } 0098 0099 /** 0100 * Return a simple flat random number (from the current 0101 * RandomGenerator object) in the range ]\a xl,\a xu[. 0102 */ 0103 template <typename Unit> 0104 static Unit rnd(Unit xl, Unit xu) { 0105 return current().rnd(xl, xu); 0106 } 0107 0108 /** 0109 * Return a true with probability \a p (default 0.5). 0110 */ 0111 static bool rndbool(double p = 0.5) { 0112 return current().rndbool(p); 0113 } 0114 0115 /** 0116 * Return a true with probability \a p (default 0.5). Uses push_back 0117 * to reuse random number. 0118 */ 0119 static bool prndbool(double p = 0.5) { 0120 return current().rndbool(p); 0121 } 0122 0123 /** 0124 * Return a true with probability \a p1/(\a p1+\a p2). 0125 */ 0126 static bool rndbool(double p1, double p2) { 0127 return current().rndbool(p1, p2); 0128 } 0129 0130 /** 0131 * Return a true with probability \a p1/(\a p1+\a p2). Uses 0132 * push_back to reuse random number. 0133 */ 0134 static bool prndbool(double p1, double p2) { 0135 return current().rndbool(p1, p2); 0136 } 0137 0138 /** 0139 * Return -1, 0, or 1 with relative probabilities \a p1, \a p2, \a 0140 * p3. 0141 */ 0142 static int rndsign(double p1, double p2, double p3) { 0143 return current().rndsign(p1, p2, p3); 0144 } 0145 0146 /** 0147 * Return -1, 0, or 1 with relative probabilities \a p1, \a p2, \a 0148 * p3. Uses push_back to reuse random number. 0149 */ 0150 static int prndsign(double p1, double p2, double p3) { 0151 return current().rndsign(p1, p2, p3); 0152 } 0153 0154 /** 0155 * Return an integer \f$i\f$ with probability p\f$i\f$/(\a p0+\a 0156 * p1). 0157 */ 0158 static int rnd2(double p0, double p1) { 0159 return current().rnd2(p0, p1); 0160 } 0161 0162 /** 0163 * Return an integer \f$i\f$ with probability p\f$i\f$/(\a p0+\a 0164 * p1+\a p2). 0165 */ 0166 static int rnd3(double p0, double p1, double p2) { 0167 return current().rnd3(p0, p1, p2); 0168 } 0169 0170 /** 0171 * Return an integer/ \f$i\f$ with probability p\f$i\f$(\a p0+\a 0172 * p1+\a p2+\a p3). 0173 */ 0174 static int rnd4(double p0, double p1, double p2, double p3) { 0175 return current().rnd4(p0, p1, p2, p3); 0176 } 0177 0178 /** 0179 * Return an integer/ \f$i\f$ with probability p\f$i\f$(\a p0+\a 0180 * p1+\a p2+\a p3+\a p4). 0181 */ 0182 static int rnd5(double p0, double p1, double p2, double p3, double p4) { 0183 return current().rnd5(p0, p1, p2, p3, p4); 0184 } 0185 0186 /** 0187 * Return a simple flat random integrer number in the range [0,\a xu[. 0188 */ 0189 static long irnd(long xu = 2) { return long(rnd() * xu); } 0190 0191 /** 0192 * Return a simple flat random integrer number in the range [\a xl,\a xu[. 0193 */ 0194 static long irnd(long xl, long xu) { return xl + irnd(xu-xl); } 0195 0196 /** 0197 * Return a number between zero and infinity, distributed according 0198 * to \f$e^-x\f$. 0199 */ 0200 static double rndExp() { return current().rndExp(); } 0201 0202 /** 0203 * Return a number between zero and infinity, distributed according 0204 * to \f$e^-{x/\mu}\f$ where \f$\mu\f$ is the \a mean value. 0205 */ 0206 template <typename Unit> 0207 static Unit rndExp(Unit mean) { return current().rndExp(mean); } 0208 0209 /** 0210 * Return a number distributed according to a Gaussian distribution 0211 * with zero mean and unit variance. 0212 */ 0213 static double rndGauss() { return current().rndGauss(); } 0214 0215 /** 0216 * Return a number distributed according to a Gaussian distribution 0217 * with a given standard deviation, \a sigma, and a given \a mean. 0218 */ 0219 template <typename Unit> 0220 static Unit rndGauss(Unit sigma, Unit mean = Unit()) { 0221 return current().rndGauss(sigma, mean); 0222 } 0223 0224 /** 0225 * Return a positive number distributed according to a 0226 * non-relativistic Breit-Wigner with a given width, \a gamma, and a 0227 * given \a mean. 0228 */ 0229 template <typename Unit> 0230 static Unit rndBW(Unit mean, Unit gamma) { 0231 return current().rndBW(mean, gamma); 0232 } 0233 0234 /** 0235 * Return a positive number distributed according to a 0236 * non-relativistic Breit-Wigner with a given width, \a gamma, and a 0237 * given \a mean. The distribution is cut-off so that the number is 0238 * between \a mean - \a cut and \a mean + \a cut 0239 */ 0240 template <typename Unit> 0241 static Unit rndBW(Unit mean, Unit gamma, Unit cut) { 0242 return current().rndBW(mean, gamma, cut); 0243 } 0244 0245 /** 0246 * Return a positive number distributed according to a relativistic 0247 * Breit-Wigner with a given width, \a gamma, and a given \a mean. 0248 */ 0249 template <typename Unit> 0250 static Unit rndRelBW(Unit mean, Unit gamma) { 0251 return current().rndRelBW(mean, gamma); 0252 } 0253 0254 /** 0255 * Return a positive number distributed according to a relativistic 0256 * Breit-Wigner with a given width, \a gamma, and a given \a 0257 * mean. The distribution is cut-off so that the number is between 0258 * \a mean - \a cut and \a mean + \a cut 0259 */ 0260 template <typename Unit> 0261 static Unit rndRelBW(Unit mean, Unit gamma, Unit cut) { 0262 return current().rndRelBW(mean, gamma, cut); 0263 } 0264 0265 /** 0266 * Return a non-negative number generated according to a Poissonian 0267 * distribution with a given \a mean. 0268 */ 0269 static long rndPoisson(double mean) { 0270 return current().rndPoisson(mean); 0271 } 0272 0273 private: 0274 0275 /** 0276 * The stack of RandomGenerators requested. 0277 */ 0278 static vector<RanGenPtr> theRandomStack; 0279 0280 /** 0281 * True if this object is responsible for pushing a RandomGenerator 0282 * onto the stack. 0283 */ 0284 bool randomPushed; 0285 0286 private: 0287 0288 /** 0289 * Private and non-existent assignment operator. 0290 */ 0291 UseRandom & operator=(const UseRandom &) = delete; 0292 0293 }; 0294 0295 } 0296 0297 #endif /* ThePEG_UseRandom_H */
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|