00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00070
00071 #ifndef _ABKRAND_H_
00072 #define _ABKRAND_H_
00073
00074 #include <stdlib.h>
00075 #include <math.h>
00076 #include <fstream>
00077 #include <limits.h>
00078 #include "abkseed.h"
00079 #include "abktimer.h"
00080 #include "abkassert.h"
00081 #include "verbosity.h"
00082 #include <vector>
00083 using std::vector;
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100 class RandomRoot
00101 {
00102 private:
00103 SeedHandler _handler;
00104 protected:
00105 unsigned _seed;
00106 Verbosity _verb;
00107 RandomRoot(unsigned seed=UINT_MAX,
00108 Verbosity verb=Verbosity("silent")):_handler(seed),
00109 _verb(verb)
00110 {
00111 _seed = _handler._seed;
00112 if (verb.forMajStats)
00113 cout << "_seed = " << _seed << endl;
00114 }
00115
00116 RandomRoot(const char* locIdent,
00117 unsigned counterOverride=UINT_MAX,
00118 Verbosity verb=Verbosity("silent"));
00119
00120 public:
00121 static const double randTick;
00122 unsigned getSeed() const
00123 {
00124 abkfatal(!_handler._isSeedMultipartite,"can't use getSeed() "
00125 "with multipartite seed");
00126 return _seed;
00127 }
00128
00129 static unsigned getExternalSeed();
00130 const char *getLocIdent() const;
00131 unsigned getCounter() const;
00132
00133 const Verbosity &getVerbosity() const {return _verb;}
00134 };
00135
00136
00137
00138
00139
00140
00141
00142
00143 class Tausworthe : public RandomRoot
00144 {
00145 private:
00146 unsigned _encryptWithSeed(unsigned clear);
00147
00148
00149
00150
00151
00152 void _encryptBufWithSeed();
00153
00154 void _encryptBufMultipartiteSeed();
00155
00156 unsigned _multmod(unsigned x,unsigned y);
00157
00158 unsigned _bufferSize;
00159 unsigned _tauswortheQ;
00160 mutable unsigned *_buffer;
00161 mutable unsigned _cursor;
00162
00163 protected:
00164 Tausworthe(unsigned bufSize, unsigned Q,
00165 unsigned *preloads,unsigned seed=UINT_MAX,
00166 Verbosity verb=Verbosity("silent"))
00167 :RandomRoot(seed,verb), _bufferSize(bufSize),
00168 _tauswortheQ(Q), _preloads(preloads)
00169 {
00170 _buffer = new unsigned[bufSize];
00171 for(unsigned i=0; i!=bufSize; i++) _buffer[i]=0;
00172 _encryptBufWithSeed();
00173 }
00174
00175 Tausworthe(unsigned bufSize, unsigned Q,
00176 unsigned *preloads,const char *locIdent,
00177 unsigned counterOverride=UINT_MAX,
00178 Verbosity verb=Verbosity("silent"))
00179 :RandomRoot(locIdent,counterOverride, verb),
00180 _bufferSize(bufSize),_tauswortheQ(Q),
00181 _preloads(preloads)
00182 {
00183 _buffer = new unsigned[bufSize];
00184 for(unsigned i=0; i!=bufSize; i++) _buffer[i]=0;
00185 _encryptBufMultipartiteSeed();
00186 }
00187
00188 ~Tausworthe(){delete [] _buffer;}
00189 inline unsigned _getRawUnsigned()
00190 {
00191
00192 {
00193 unsigned retval;
00194 int otherPoint = _cursor-_tauswortheQ;
00195 if (otherPoint<0)
00196 otherPoint += _bufferSize;
00197
00198 #if defined(__SUNPRO_CC)
00199
00200
00201 retval = const_cast<unsigned*>(_buffer)
00202 [const_cast<Tausworthe*>(this)->_cursor] ^=
00203 const_cast<unsigned*>(_buffer)[otherPoint];
00204 if (++(const_cast<Tausworthe*>(this)->_cursor) >= _bufferSize)
00205 const_cast<Tausworthe*>(this)->_cursor=0;
00206
00207 #else
00208 retval = _buffer[_cursor] ^= _buffer[otherPoint];
00209 if (++_cursor >= _bufferSize)
00210 _cursor=0;
00211 #endif
00212
00213 return retval;
00214 }
00215
00216 }
00217 double _getRawDouble() {return randTick * _getRawUnsigned();}
00218 unsigned *_preloads;
00219
00220 };
00221
00222
00223
00224
00225
00226 class RandomKernel250 : public Tausworthe
00227 {
00228
00229 protected:
00230 RandomKernel250(unsigned seed=UINT_MAX,
00231 Verbosity verb=Verbosity("silent"));
00232
00233 RandomKernel250(const char *locIdent,
00234 unsigned counterOverride=UINT_MAX,
00235 Verbosity verb=Verbosity("silent"));
00236
00237 };
00238
00239 class RandomKernel1279 : public Tausworthe
00240 {
00241
00242 protected:
00243 RandomKernel1279(unsigned seed=UINT_MAX,
00244 Verbosity verb=Verbosity("silent"));
00245
00246
00247 RandomKernel1279(const char *locIdent,
00248 unsigned counterOverride=UINT_MAX,
00249 Verbosity verb=Verbosity("silent"));
00250 };
00251
00252
00253
00254 template <class RK> class RandomNumberGeneratorT: public RK
00255 {
00256 protected:
00257
00258 double _dLowerB, _dUpperB, _dDelta;
00259 unsigned _lowerB, _upperB, _delta;
00260
00261 unsigned _getUnsignedRand() ;
00262 double _getDoubleRand() ;
00263
00264 public:
00265
00266
00267 RandomNumberGeneratorT(double _lowerBdry, double _upperBdry,
00268 unsigned seedN=UINT_MAX,
00269 Verbosity verb=Verbosity("silent"))
00270 : RK(seedN,verb),
00271 _dLowerB(_lowerBdry),_dUpperB(_upperBdry),
00272 _dDelta(_upperBdry-_lowerBdry),
00273 _lowerB(unsigned(_lowerBdry)),
00274 _upperB(unsigned(_upperBdry))
00275 {
00276 abkfatal(_lowerBdry<_upperBdry," Invalid range for random number generator");
00277 abkfatal(_dDelta>1e-4," Range too small for random number generator ");
00278 _delta=unsigned(_dDelta);
00279 if (_dDelta<1) _delta=1;
00280 }
00281
00282 RandomNumberGeneratorT(double _lowerBdry, double _upperBdry,
00283 const char *locIdent,
00284 unsigned counterOverride=UINT_MAX,
00285 Verbosity verb=Verbosity("silent"))
00286 :
00287 RK(locIdent,counterOverride,verb),
00288 _dLowerB(_lowerBdry),_dUpperB(_upperBdry),
00289 _dDelta(_upperBdry-_lowerBdry),
00290 _lowerB(unsigned(_lowerBdry)),
00291 _upperB(unsigned(_upperBdry))
00292 {
00293 abkfatal(_lowerBdry<_upperBdry," Invalid range for random number generator");
00294 abkfatal(_dDelta>1e-4," Range too small for random number generator ");
00295 _delta=unsigned(_dDelta);
00296 if (_dDelta<1) _delta=1;
00297 }
00298
00299 unsigned operator()(unsigned N) {return RK::_getRawUnsigned()%N;}
00300 };
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321 template <class RK> class RandomUnsignedT : public RandomNumberGeneratorT<RK>
00322 {
00323 public:
00324 RandomUnsignedT(double _lowerBdry, double _upperBdry,
00325 unsigned seedN=UINT_MAX,
00326 Verbosity verb=Verbosity("silent")) :
00327 RandomNumberGeneratorT<RK>(_lowerBdry,_upperBdry,seedN,verb) {}
00328
00329 RandomUnsignedT(double _lowerBdry, double _upperBdry,
00330 const char *locIdent, unsigned counterOverride=UINT_MAX,
00331 Verbosity verb=Verbosity("silent")) :
00332 RandomNumberGeneratorT<RK>(_lowerBdry,_upperBdry,
00333 locIdent,counterOverride,verb) {}
00334 operator unsigned() {return RandomNumberGeneratorT<RK>::_getUnsignedRand();}
00335 };
00336
00337
00338
00339
00340
00341 template <class RK> class RandomDoubleT : public RandomNumberGeneratorT<RK>
00342 {
00343 public:
00344 RandomDoubleT(double _lowerBdry, double _upperBdry,
00345 unsigned seedN=UINT_MAX,
00346 Verbosity verb=Verbosity("silent")):
00347 RandomNumberGeneratorT<RK>(_lowerBdry,_upperBdry,seedN,verb) {};
00348
00349 RandomDoubleT(double _lowerBdry, double _upperBdry,
00350 const char *locIdent,unsigned counterOverride=UINT_MAX,
00351 Verbosity verb=Verbosity("silent")):
00352 RandomNumberGeneratorT<RK>(_lowerBdry,_upperBdry,
00353 locIdent,counterOverride,verb) {};
00354 operator double() {return RandomNumberGeneratorT<RK>::_getDoubleRand();}
00355 };
00356
00357
00358
00359
00360
00361
00362
00363
00364 template <class RK> class RandomRawUnsignedT : public RK
00365 {
00366 public:
00367 RandomRawUnsignedT(unsigned seedN=UINT_MAX,
00368 Verbosity verb=Verbosity("silent")) :
00369 RK(seedN,verb) {};
00370
00371 RandomRawUnsignedT(const char *locIdent,
00372 unsigned counterOverride=UINT_MAX,
00373 Verbosity verb=Verbosity("silent")) :
00374 RK(locIdent,counterOverride,verb) {};
00375
00376
00377 operator unsigned() {return RK::_getRawUnsigned();}
00378 unsigned operator()(unsigned N) {return RK::_getRawUnsigned()%N;}
00379 };
00380
00381
00382 template <class RK> class RandomRawDoubleT : public RK
00383 {
00384 public:
00385
00386 RandomRawDoubleT(unsigned seedN=UINT_MAX,
00387 Verbosity verb=Verbosity("silent")) :
00388 RK(seedN,verb) {};
00389
00390 RandomRawDoubleT(const char *locIdent,
00391 unsigned counterOverride=UINT_MAX,
00392 Verbosity verb=Verbosity("silent")) :
00393 RK(locIdent,counterOverride,verb) {};
00394
00395
00396 operator double() {return RK::_getRawDouble();}
00397 unsigned operator()(unsigned N) {return RK::_getRawUnsigned()%N;}
00398 };
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410 template <class RK> class RandomNormalT : public RK
00411 {
00412 private:
00413 double _mu;
00414 double _sigma;
00415 mutable bool _cacheFull;
00416 mutable double _cachedValue;
00417 public:
00418 RandomNormalT<RK>(double mean,
00419 double stdDev,
00420 unsigned seed=UINT_MAX,
00421 Verbosity verb=Verbosity("silent"))
00422 : RK(seed,verb),
00423 _mu(mean),
00424 _sigma(stdDev),
00425 _cacheFull(false),
00426 _cachedValue(0.0)
00427 {}
00428
00429 RandomNormalT<RK>(double mean,
00430 double stdDev,
00431 const char *locIdent,
00432 unsigned counterOverride=UINT_MAX,
00433 Verbosity verb=Verbosity("silent"))
00434 :
00435 RK(locIdent,counterOverride,verb),
00436 _mu(mean),
00437 _sigma(stdDev),
00438 _cacheFull(false),
00439 _cachedValue(0.0)
00440 {}
00441
00442 operator double();
00443
00444
00445 };
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456 template <class RK> class RandomNormCorrPairsT
00457 {
00458 private:
00459 RandomNormalT<RK> _norm;
00460 const double _mu1,_mu2;
00461 const double _sigma1,_sigma2;
00462 const double _rho;
00463 const double _c;
00464
00465
00466 const double _a1,_a2,_b1,_b2;
00467
00468 public:
00469 RandomNormCorrPairsT<RK>(double mean1,double stdDev1,
00470 double mean2, double stdDev2,
00471 double correlation,
00472 unsigned seed=UINT_MAX,
00473 Verbosity verb=Verbosity("silent"))
00474 : _norm(0,1,seed,verb),
00475 _mu1(mean1),_mu2(mean2),
00476 _sigma1(stdDev1),_sigma2(stdDev2),
00477 _rho(correlation),
00478 _c(sqrt((1-_rho)/(1+_rho))),
00479 _a1(_sigma1/sqrt(1+_c*_c)),
00480 _a2(_sigma2/sqrt(1+_c*_c)),
00481 _b1(_sigma1*_c/sqrt(1+_c*_c)),
00482 _b2(_sigma2*_c/sqrt(1+_c*_c))
00483 {}
00484
00485 RandomNormCorrPairsT<RK>(double mean1,double stdDev1,
00486 double mean2, double stdDev2,
00487 double correlation,
00488 const char *locIdent,
00489 unsigned counterOverride=UINT_MAX,
00490 Verbosity verb=Verbosity("silent"))
00491 : _norm(0,1,locIdent,counterOverride,verb),
00492 _mu1(mean1),_mu2(mean2),
00493 _sigma1(stdDev1),_sigma2(stdDev2),
00494 _rho(correlation),
00495 _c(sqrt((1-_rho)/(1+_rho))),
00496 _a1(_sigma1/sqrt(1+_c*_c)),
00497 _a2(_sigma2/sqrt(1+_c*_c)),
00498 _b1(_sigma1*_c/sqrt(1+_c*_c)),
00499 _b2(_sigma2*_c/sqrt(1+_c*_c))
00500 {}
00501
00502 std::pair<double,double> getPair();
00503
00504
00505 };
00506
00507
00508
00509 template <class RK> class RandomNormCorrTuplesT
00510 {
00511 private:
00512
00513
00514
00515
00516 bool _findBasis(const vector<vector<double> > &_rho_ij,
00517 vector<vector<double> > &_v_ij);
00518
00519 const unsigned _n;
00520 vector<RandomNormalT<RK>*> _norm_j;
00521 vector<double> _y_j;
00522
00523 const vector<double> _mu_i;
00524 const vector<double> _sigma_i;
00525 vector<vector<double> > _v_ij;
00526
00527 bool _bad;
00528
00529 public:
00530 RandomNormCorrTuplesT<RK>(const vector<double> &means,
00531 const vector<double> &stdDevs,
00532
00533
00534
00535
00536
00537 const vector<vector<double> > &corrs,
00538 const char *locIdent,
00539 unsigned counterOverride=UINT_MAX,
00540 Verbosity verb=Verbosity("silent"));
00541
00542 ~RandomNormCorrTuplesT<RK>();
00543
00544 void getTuple(vector<double> &tuple);
00545 bool bad() const {return _bad;}
00546 };
00547
00548
00549
00550
00551
00552
00553
00554 #include "abkrand_templ.cxx"
00555
00556 typedef RandomDoubleT<RandomKernel250> RandomDouble;
00557 typedef RandomDoubleT<RandomKernel1279> RandomDouble1279;
00558 typedef RandomUnsignedT<RandomKernel250> RandomUnsigned;
00559 typedef RandomUnsignedT<RandomKernel1279> RandomUnsigned1279;
00560 typedef RandomRawDoubleT<RandomKernel250> RandomRawDouble;
00561 typedef RandomRawDoubleT<RandomKernel1279> RandomRawDouble1279;
00562 typedef RandomRawUnsignedT<RandomKernel250> RandomRawUnsigned;
00563 typedef RandomRawUnsignedT<RandomKernel1279> RandomRawUnsigned1279;
00564 typedef RandomNormalT<RandomKernel250> RandomNormal;
00565 typedef RandomNormalT<RandomKernel1279> RandomNormal1279;
00566 typedef RandomNumberGeneratorT<RandomKernel250> RandomNumberGenerator;
00567 typedef RandomNumberGeneratorT<RandomKernel1279> RandomNumberGenerator1279;
00568 typedef RandomNumberGenerator RNG;
00569 typedef RandomNumberGenerator1279 RNG1279;
00570 typedef RandomNormCorrPairsT<RandomKernel250> RandomNormCorrPairs;
00571 typedef RandomNormCorrPairsT<RandomKernel1279> RandomNormCorrPairs1279;
00572 typedef RandomNormCorrTuplesT<RandomKernel250> RandomNormCorrTuples;
00573 typedef RandomNormCorrTuplesT<RandomKernel1279> RandomNormCorrTuples1279;
00574
00575
00576 #endif