1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
|
#include "CLHEP/Random/Randomize.h"
#include "CLHEP/Random/defs.h"
#include <iostream>
#include "CLHEP/Random/RandGaussQ.h"
#include "CLHEP/Random/RandGaussT.h"
#include "CLHEP/Random/RandPoissonQ.h"
#include "CLHEP/Random/RandPoissonT.h"
#include "CLHEP/Random/RandBit.h"
#include "CLHEP/Units/PhysicalConstants.h"
using std::cin;
using std::cout;
using std::cerr;
using std::endl;
using namespace CLHEP;
//#ifndef _WIN32
//using std::exp;
//#endif
// ---------
// RandGauss
//
// mf 12/13/04 Correction in way engines are supplied to RandBit() ctor
// (gcc3.3.1 exposed previously innocuous mistake)
// ---------
double gammln1(double xx) {
// Returns the value ln(Gamma(xx) for xx > 0. Full accuracy is obtained for
// xx > 1. For 0 < xx < 1. the reflection formula (6.1.4) can be used first.
// (Adapted from Numerical Recipes in C)
static double cof[6] = {76.18009172947146,-86.50532032941677,
24.01409824083091, -1.231739572450155,
0.1208650973866179e-2, -0.5395239384953e-5};
int j;
double x = xx - 1.0;
double tmp = x + 5.5;
tmp -= (x + 0.5) * std::log(tmp);
double ser = 1.000000000190015;
for ( j = 0; j <= 5; j++ ) {
x += 1.0;
ser += cof[j]/x;
}
return -tmp + std::log(2.5066282746310005*ser);
}
double gammln2(double xx) {
// Returns the value ln(Gamma(xx) for xx > 0. Full accuracy is obtained for
// xx > 1. For 0 < xx < 1. the reflection formula (6.1.4) can be used first.
// (Adapted from Numerical Recipes in C)
static double cof[6] = {76.18009172947146,-86.50532032941677,
24.01409824083091, -1.231739572450155,
0.1208650973866179e-2, -0.5395239384953e-5};
int j;
double x = xx - 0.0;
double tmp = x + 5.5;
tmp -= (x + 0.5) * std::log(tmp);
double ser = 1.000000000190015;
for ( j = 0; j <= 5; j++ ) {
x += 1.0;
ser += cof[j]/x;
}
return -tmp + std::log(2.5066282746310005*ser/xx);
}
#include <iomanip>
int main() {
cout << "Enter 1 for RandGauss, 2 for RandGaussQ, 3 for DualRand flat: ";
int choice;
cin >> choice;
if (choice==1) {
cout << "\n--------------------------------------------\n";
cout << "Test of Gauss distribution speed\n\n";
long seed;
cout << "Please enter an integer seed: ";
cin >> seed;
int nNumbers;
cout << "How many numbers should we generate: ";
cin >> nNumbers;
cout << "\nInstantiating distribution utilizing DualRand engine...\n";
DualRand eng (seed);
RandGauss dist (eng);
double sum = 0;
for (int i=0; i < nNumbers; i++) {
sum += dist.fire();
}
cout << "\n Finished: sum is " << sum << " \n";
}
if (choice==2) {
cout << "\n--------------------------------------------\n";
cout << "Test of RandGaussQ distribution speed\n\n";
long seed;
cout << "Please enter an integer seed: ";
cin >> seed;
int nNumbers;
cout << "How many numbers should we generate: ";
cin >> nNumbers;
cout << "\nInstantiating distribution utilizing DualRand engine...\n";
DualRand eng (seed);
RandGaussQ dist (eng);
double sum = 0;
for (int i=0; i < nNumbers; i++) {
sum += dist.fire();
}
cout << "\n Finished: sum is " << sum << " \n";
}
if (choice==3) {
cout << "\n--------------------------------------------\n";
cout << "Test of DualRand flat speed\n\n";
long seed;
cout << "Please enter an integer seed: ";
cin >> seed;
int nNumbers;
cout << "How many numbers should we generate: ";
cin >> nNumbers;
cout << "\nInstantiating distribution utilizing DualRand engine...\n";
DualRand eng (seed);
double sum = 0;
for (int i=0; i < nNumbers; i++) {
sum += eng.flat();
}
cout << "\n Finished: sum is " << sum << " \n";
}
#ifdef GAMMA
cout << "\nNow we will compute the first 20 gammas, using gammln:\n";
double x;
for (x=1; x <= 20; x+=1) {
cout << x << std::setprecision(20) << " " << std::exp(gammln1(x))
<< " " << std::exp(gammln2(x)) << " difference in gammln2 = " <<
gammln1(x)-gammln2(x) << "\n";
}
cout << "\nNow we will compute gamma of small numbers: \n";
for ( x=1; x > .000000001; x *= .9 ) {
cout << x << std::setprecision(20) << " " <<
1 - std::exp(gammln1(x)) * std::exp(gammln1(2-x)) * std::sin(CLHEP::pi*(1-x)) / (CLHEP::pi*(1-x)) <<
" " <<
1 - std::exp(gammln2(x)) * std::exp(gammln1(2-x)) * std::sin(CLHEP::pi*(1-x)) / (CLHEP::pi*(1-x)) <<
"\n";
}
#endif // GAMMA
#ifdef POISSON
cout << "\n--------------------------------------------\n";
cout << "Test of Poisson distribution speed\n\n";
long seed;
cout << "Please enter an integer seed: ";
cin >> seed;
double mu;
cout << "Please enter mu: ";
cin >> mu;
int nNumbers;
cout << "How many numbers should we generate: ";
cin >> nNumbers;
cout << "\nInstantiating distribution utilizing DualRand engine...\n";
DualRand eng (seed);
RandPoisson dist (eng, mu);
// RandFlat dist (eng);
double sum = 0;
for (int i=0; i < nNumbers; i++) {
sum += dist.fire();
// sum += dist.quick();
// sum += dist.fire(mu);
// sum += dist.quick(mu);
}
cout << "\n Finished: sum is " << sum << " \n";
#endif // POISSON
#define MISC
#ifdef MISC
DualRand e;
// RandGauss usage modes
cout << "testing RandGaussT::shoot(): " << RandGaussT::shoot() << "\n";
cout << "testing RandGaussT::shoot(&e): " << RandGaussT::shoot(&e) << "\n";
cout << "testing RandGaussT::shoot(100,10): " <<
RandGaussT::shoot(100,10) << "\n";
cout << "testing RandGaussT::shoot(&e,100,10): " <<
RandGaussT::shoot(&e,100,10) << "\n";
RandGaussT gt (e, 50,2);
cout << "testing gt.fire(): " << gt.fire() << "\n";
cout << "testing gt.fire(200,2): " << gt.fire(200,2) << "\n";
cout << "testing RandGaussQ::shoot(): " << RandGaussQ::shoot() << "\n";
cout << "testing RandGaussQ::shoot(&e): " << RandGaussQ::shoot(&e) << "\n";
cout << "testing RandGaussQ::shoot(100,10): " <<
RandGaussQ::shoot(100,10) << "\n";
cout << "testing RandGaussQ::shoot(&e,100,10): " <<
RandGaussQ::shoot(&e,100,10) << "\n";
RandGaussQ qt (e, 50,2);
cout << "testing qt.fire(): " << qt.fire() << "\n";
cout << "testing qt.fire(200,2): " << qt.fire(200,2) << "\n";
// RandPoisson usage modes
cout << "testing RandPoissonT::shoot(): " << RandPoissonT::shoot() << "\n";
cout << "testing RandPoissonT::shoot(&e): "
<< RandPoissonT::shoot(&e) << "\n";
cout << "testing RandPoissonT::shoot(90): " <<
RandPoissonT::shoot(90) << "\n";
cout << "testing RandPoissonT::shoot(&e,90): " <<
RandPoissonT::shoot(&e,90) << "\n";
RandPoissonT pgt (e,50);
cout << "testing pgt.fire(): " << pgt.fire() << "\n";
cout << "testing pgt.fire(20): " << pgt.fire(20) << "\n";
cout << "testing RandPoissonQ::shoot(): " << RandPoissonQ::shoot() << "\n";
cout << "testing RandPoissonQ::shoot(&e): " << RandPoissonQ::shoot(&e) << "\n";
cout << "testing RandPoissonQ::shoot(90): " <<
RandPoissonQ::shoot(90) << "\n";
cout << "testing RandPoissonQ::shoot(&e,90): " <<
RandPoissonQ::shoot(&e,90) << "\n";
RandPoissonQ pqt (e,50);
cout << "testing pqt.fire(): " << pqt.fire() << "\n";
cout << "testing pqt.fire(20): " << pqt.fire(20) << "\n";
// RandBit modes coming from RandFlat and bit modes
cout << "testing RandBit::shoot(): " << RandBit::shoot() << "\n";
cout << "testing RandBit::shoot(&e): " << RandBit::shoot(&e) << "\n";
cout << "testing RandBit::shoot(10,20): " << RandBit::shoot(10,20) << "\n";
cout << "testing RandBit::shoot(&e,10,20): "<<
RandBit::shoot(&e,10,20) << "\n";
RandBit b ( e, 1000, 1100 );
cout << "testing b.fire(): " << b.fire() << "\n";
cout << "testing b.fire(10,20): " << b.fire(10,20) << "\n";
int i;
cout << "testing RandBit::shootBit(): ";
for (i=0; i<40; i++) {
cout << RandBit::shootBit();
} cout << "\n";
cout << "testing RandBit::shootBit(&e): ";
for (i=0; i<40; i++) {
cout << RandBit::shootBit(&e);
} cout << "\n";
cout << "testing RandBit::fireBit(): ";
for (i=0; i<40; i++) {
cout << b.fireBit();
} cout << "\n";
// Timing for RandBit:
cout << "Timing RandFlat::shootBit(): Enter N: ";
int N;
cin >> N;
int sum=0;
for (i=0; i<N; i++) {
sum+= RandFlat::shootBit();
}
cout << "--------- Done.............. Sum = " << sum << "\n";
cout << "Timing RandBit::shootBit(): Enter N: ";
cin >> N;
sum = 0;
for (i=0; i<N; i++) {
sum += RandBit::shootBit();
}
cout << "--------- Done.............. Sum = " << sum << "\n";
#endif // MISC
return 0;
}
|